A list is an object that contains multiple data items. The items in a list are ordered, and each item has an index indicating its position in the list. The first item in a list is at index 0, the second at index 1, and so on.
The data items are also called elements. The elements of a list can be any type. Lists are dynamic data structures, meaning that items may be added to them or removed from them. Lists are mutable, which means that their contents can be changed during a program’s execution.
numbers = [10, 20, 30, 40]
cities = ['Delhi', 'Mumbai', 'Chennai', 'Kolkata']
add = [8, 'Deepak', 'India', 9210458408]
x = ['CBSE', 2020, [11, 12]]
There is a special list that contains no elements. It is called the empty list, and it is denoted [].
empty = []
The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string the bracket operator ([]). The expression inside the brackets specifies the index. Remember that the indices start at 0:
>>> num = [10, 20, 30, 40]
>>> print(num[0])
10
>>> a = num[1] + num[3]
>>> print(a)
60
If you try to read or write an element that does not exist, you get a runtime error:
>>> num[4] = 50
Traceback (most recent call last):
File "", line 1, in
num[4] = 50
IndexError: list index out of range
If an index has a negative value, it counts backward from the end of the list:
>>> print(num[-1])
40
>>> print(num[-4])
10
>>> print(num[-5])
Traceback (most recent call last):
File "", line 1, in
print(num[-5])
IndexError: list index out of range
Lists in Python are mutable, which means their elements can be changed.
>>> colors = ['red','green','yellow']
>>> colors[1] = 'blue'
>>> print(colors)
['red', 'blue', 'yellow']
A slice is a span of items that are taken from a sequence. To get a slice of a list, you write an expression in the following general format:
list_name[start : end]
In the general format, start is the index of the first element in the slice, and end is the index marking the end(but not including) of the slice. If you omit the first index, the slice starts at the beginning. If you omit the second, the slice goes to the end. So if you omit both, the slice is a copy of the whole list.
>>> t = [10, 20, 30, 40, 50, 60]
>>> t[1:3]
[20, 30]
>>> t[:4]
[10, 20, 30, 40]
>>> t[3:]
[40, 50, 60]
>>> t[:]
[10, 20, 30, 40, 50, 60]
You can also use the step value in slicing. Syntax is given below:
listObject[start:end:step]
>>> t = [10, 20, 30, 40, 50, 60]
>>> t[::2]
[10, 30, 50]
>>> t[1::2]
[20, 40, 60]
>>> t[:4:2]
[10, 30]
#To reverse list use negative step
>>> t[::-1]
[60, 50, 40, 30, 20, 10]
The + operator concatenates lists:
>>> list1 = [10,20,30]
>>> list2 = [7,8,60]
>>> list3 = list1 + list2
>>> print(list3)
[10, 20, 30, 7, 8, 60]
The * operator repeats a list a given number of times:
>>> a = [20,30,40]
>>> b = a*2
>>> print(b)
[20, 30, 40, 20, 30, 40]
in is a Boolean operator that tests membership in a sequence.
>>> fruits = ['orange', 'apple', 'mango', 'banana']
>>> 'mango' in fruits
True
>>> 'cherry' in fruits
False
>>> 'cherry' not in fruits
True
To check whether two variables refer to the same object, you can use the is operator.
>>> a = [1, 2, 3]
>>> b = a
In this case, the state diagram looks like this:
>>> b is a
True
del removes an element from a list:
>>> a = ['one', 'two', 'three']
>>> del a[1]
>>> a
['one', 'three']
>>> b = ['Sun', 'Mon', 'Tue', ’Wed’]
>>> del b[-1]
>>> b
['Sun', 'Mon', 'Tue']
You can use a slice as an index for del:
>>> list = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del list[1:5]
>>> print list
['a', 'f']