Python list Function

Python list is similar to array in other programming languages. However, Python list items can be of different type. Python list allows various operations which are very powerful and convenient. List initialization:

>>> x = ["red","green","blue"]
>>> x
["red","green","blue"]
>>> len(x)
3
Items of list can be of different type:
>>> x = ["red","green","blue", 4, 9]
>>> x
["red","green","blue", 4, 9]
Access list items:
>>> x[0]
'red'
>>> x[2]
'blue'
>>> x[-1]
9
>>> x[1:-1]
['green','blue',4]
>>> x[1:-2]
['green','blue']
Update list item value:
>>> x[1] = 7
>>> x
["red", 7, "blue", 4, 9]
>>> x[3] += 2
>>> x
["red","green","blue", 6, 9]
Append list:
>>> x += [10]
>>> x
["red","green","blue", 6, 9, 10]
>>> x[:3] + ["yellow","gray"]
['red', 7, 'blue', 'yellow', 'gray']
Remove and insert list items:
>>> x = ["red",7,"blue", 6, 9, 10]
>>> del x[1]
>>> x
["red","blue", 6, 9, 10]
>>> x[0:2] = []
>>> x
[6,9,10]
>>> x[0:2] = [1,2,3]
>>> x
[1,2,3,10]
>>> x[3:3] = [4,4]
>>> x
[1,2,3,4,4,10]
Two dimension list:
>>> x = [[1,2,3],[4,5,6],["red","green","blue"]]
>>> x[1][2]
6