list Data TypeThe predefined data type list is a collection data type.
A list is an ordered collection of elements.
The elements of a list have a position index starting with the value 0.
A list variable consists of elements separated by commas and delimited by square brackets [ ].
Example
data = [2, 3, 5, 7]
The list data type is conceptually similar to the array type, but unlike an array, the elements of a list can be of different types.
A Python list is bounded and iterable: you can iterate over these elements in a for loop.
Nested list
A list can have one element of type list.
Access
You access the elements of a list using the [ ] and [:] operators.
Operations
The concatenation operator is the symbol +
The repetition operator is the symbol *
The membership operator is in
The non-membership operator is not in
Expression
Result
Description
[1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
Concatenate
['A'] * 3
['A', 'A', 'A']
Repetition
3 in [1, 2, 3]
True
Membership
Example: Fibonacci sequence
data = [1,2]
for k in range(2,11,1) :
data += [data[k-1] + data[k-2]]
print(data)
The append() method allows you to add elements to the end of a list.
Example
fruits = ['apple', 'banana', 'orange']
print('Original List:')
print(fruits)
# using append method
fruits.append('cherry')
print('Updated List:')
print(fruits)
The insert() method allows you to insert an element into a list at a given index (position).
Example
fruits = ['apple', 'banana', 'orange']
print('Original List:')
print(fruits)
# insert 'cherry' at index 2
fruits.insert(2, 'cherry')
print('Updated List:')
print(fruits)
The extend() method allows you to add the elements of a list to another list.
Example
fruits = ['apple', 'banana', 'orange']
print('Original List:')
print(fruits)
more_fruits = ['cherry', 'kiwi', 'peach']
# adding elements of one list to another
fruits.extend(more_fruits)
print('Updated List:')
print(fruits)
The assignment operator = is used to assign a value to an element of a list.
Example
colors = ['Red', 'Green', 'Yellow']
print('Original List:',)
print(colors)
# changing the third item to 'Blue'
colors[2] = 'Blue'
print('Updated List:')
print(colors)
The remove() method allows you to remove an element from a list.
Example
colors = ['Red', 'Green', 'Yellow', 'Blue']
print('Original List:')
print(colors)
# remove item Yellow from the list
colors.remove('Yellow')
print('Updated List:')
print(colors)
The pop() method is used to remove and return an element of specified index from a list.
Example
colors = ['Red', 'Green', 'Yellow', 'Blue']
print('Original List:')
print(colors)
# remove item Yellow from the list
element = colors.pop(2)
print('Updated List:')
print(colors)
print('popped element')
print(element)
The clear() method allows you to remove all elements from a list.
Example
colors = ['Red', 'Green', 'Blue']
print('Original List:')
print(colors)
# remove all items from the list
colors.clear()
print('Updated List:')
print(colors)
The del statement allows you to delete one or more elements from a list.
Example
colors = ['Red', 'Green', 'Blue', 'Cyan', 'Magenta', 'Yellow', 'Black']
print('Original List:')
print(colors)
# delete 4th item
del colors[3]
print('Updated List:')
print(colors)
# delete items from 4th to 6th
del colors[3:6]
print('Updated List:')
print(colors)
The del statement can be used to remove all elements from a list.
Example
colors = ['Red', 'Green', 'Blue']
print('Original List:')
print(colors)
# remove all items from the list
del colors[:]
print('Updated List:')
print(colors)
The length of a list is the number of elements that make up the list.
The len() function gives the length of a list.
Example
colors = ['Red', 'Green', 'Blue']
print('Total elements:', len(colors))
A list is limited and iterable: we can iterate through its elements in a for loop.
We use the membership operator in to iterate through the elements of a list.
Example
fruits = ['apple', 'banana', 'orange']
# iterate through the list
for fruit in fruits:
print(fruit)
The sort() method is used to sort a list.
Example
primes = [11, 3, 7, 5, 2]
# sort the list in ascending order
primes.sort()
print(primes)
List comprehension is an efficient way to create lists.
It allows you to generate a new list by applying an expression to each item in an iterable, optionally including a condition to filter elements.
Example
primes_set = {2, 3, 5, 7, 11, 13, 17, 19}
# create list using list comprehension
primes_list= [element for element in primes_set]
print(primes_list)
The following are the methods specifically designed for adding new item/items into a list.
Method
Description
list.append(obj)
Appends object obj to list.
list.extend(seq)
Appends the contents of sequence seq to list.
list.insert(index, obj)
Inserts object obj into list at offset index.
The following are the methods specifically designed for removing items from a list.
Method
Description
list.clear()
Clears all the contents of the list.
list.pop(obj=list[-1])
Removes and returns the last object or the object at the specified index from the list.
list.remove(obj)
Removes the first occurrence of object obj from the list.
The following are the methods used for finding or counting items in a list.
Method
Description
list.index(obj)
Returns the lowest index in list that obj appears.
list.count(obj)
Returns count of how many times obj occurs in the list.
The following are the methods used for creating copies and arranging items in a list.
Method
Description
list.copy()
Returns a copy of the list object.
list.sort([func])
Sorts the objects in the list in place, using a comparison function if provided.
list.reverse()
Reverses the order of objects in the list in place.