The list data type in python allows more operations compared to the other data types. The items in the lists are enclosed by brackets '[]'. It may contain numbers, srtings, tuples or lists (nesting of lists).
>>> list1 = [1,2,3,'how','are','you']
>>> list1
[1, 2, 3, 'how', 'are', 'you']
>>> list1[3]
'how'
>>> list1[3:]
['how', 'are', 'you']
>>> list1[:2]
[1, 2]
>>> list1[:2] = 'and'
>>> list1
['a', 'n', 'd', 3, 'how', 'are', 'you']
>>> list1[:2] = ['and']
>>> list1
['and', 3, 'how', 'are', 'you']
>>> list1 = [12,34,56,'s','y']
>>> list1
[12, 34, 56, 's', 'y']
>>> list2 = [22,'22']
We can add members of one list to the members of another list.
>>> list1[3:3] = list2
>>> list1
[12, 34, 56, 22, '22', 's', 'y']
>>> list3 = [33,list2,'33']
The following list is nested.
>>> list3
[33, [22, '22'], '33']
>>> list3[1][1]
'22'
>>> list3[1][0]
22
The del statement :
It is used to remove an element from a list when the index of that element is given. It can be used to clear the entire list or remove the variable.
No comments:
Post a Comment