List
Lists are mutable
Elements with a list can be deleted, shifted, updated or modified after the list has been created. You can change each element of a list or append new elements to a list.
You can use assignment operator (=
) to change an item or a range of items.
Updating a single element
A single value in a list can be replaced by indexing and assignment operator.
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list[5] = 2020
Output:
[2, 3, 4, 5, 5, 2020, 8, 1, 5, 6, 2, 9, 0]
You can use negative index too.
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list[-3] = 2020
Output:
[2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2020, 9, 0]
Updating multiple elements
Multiple elements can be update by specifying the elements index.
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list[5:8] = [2018, 2019, 2020]
Output:
[2, 3, 4, 5, 5, 2018, 2019, 2020, 5, 6, 2, 9, 0]
Deleting a single element
An element or elements can be deleted using del
keyword.
Delete an element
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] del my_list[5]
Output:
[2, 3, 4, 5, 5, 8, 1, 5, 6, 2, 9, 0]
Delete multiple elements
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] del my_list[3:6]
Output:
[2, 3, 4, 8, 1, 5, 6, 2, 9, 0]
First example deletes element [7]
, second example deletes three elements [5, 5, 7]
.
Prepending or appending elements to a list
An element or elements can be added to the start or end of a list using the +
operator. This is also called concatenation.
Prepend
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] [343, 564] + my_list
Output:
[343, 564, 2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2, 9, 0]
Append
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list + [343, 564]
Output:
[2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2, 9, 0, 343, 564]
If you want to add a single element, we have to wrap it in square brackets []
. Otherwise, Python raises a TypeError
.
my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list = 22 + my_list
Output:
TypeError Traceback (most recent call last)<ipython-input-11-57d83415c5aa> in <module>() 1 my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] 2 ----> 3 my_list = 22 + my_list TypeError: unsupported operand type(s) for +: \'int\' and \'list\'
The =
assignment operator can be used to save the prepending or appending elements.
Prepend
# Elements addition my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list = [343, 564] + my_list print(my_list)
Output:
[343, 564, 2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2, 9, 0]
Append
# Elements addition my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list = my_list + [343, 564] print(my_list)
Output:
[2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2, 9, 0, 343, 564]
The +=
augmented assignment operator works only when we append elements to a list
Prepend
# Elements addition my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] [343, 564] += my_list
Output:
File \"<ipython-input-9-3ec7d1771475>\", line 3 [343, 564] += my_list ^ SyntaxError: can\'t assign to literal
Append
# Elements addition my_list = [2, 3, 4, 5, 5, 7, 8, 1,5, 6, 2, 9, 0] my_list += [343, 564] print(my_list)
Output:
[2, 3, 4, 5, 5, 7, 8, 1, 5, 6, 2, 9, 0, 343, 564]
The *
operator repeats a list for a given number of times.
my_cities = [\'Krakow\', \'Warsaw\', \'Lodz\', \'Kielce\'] my_cities * 3
Output:
[\'Krakow\', \'Warsaw\', \'Lodz\', \'Kielce\', \'Krakow\', \'Warsaw\', \'Lodz\', \'Kielce\', \'Krakow\', \'Warsaw\', \'Lodz\', \'Kielce\']