Professional Documents
Culture Documents
Lists in Python
Lists in Python
Lists in Python
A list is an ordered and mutable Python container, being one of the most common
data structures in Python. To create a list, the elements are placed inside square
brackets ([]), separated by commas. Lists can contain elements of different types as
well as duplicated elements. List is a compound data type which means you can have
different-2 data types under a list, for example we can have integer, float and string
items in a same list.
Example
thislist=["apple","banana","cherry"]
print(thislist)
Example
Lists allow duplicate values:
Example
String, int and boolean data types:
Example
A list with strings, integers and boolean values:
Syntax : list.append(element)
Example
Add a list to a list:
Example
Insert the value "orange" as the second element of the fruit list:
fruits.insert(1, "orange")
Example
Add the elements of cars to the fruits list:
fruits.extend(cars)
Example
Add a tuple to the fruits list:
points = (1, 4, 5, 9)
fruits.extend(points)
Removing Elements from a List
The remove() method removes the first occurrence of the element with the
specified value.
Example
Remove the "banana" element of the fruit list:
fruits.remove("banana")
Example
Remove the second element of the fruit list:
fruits.pop(1)