List in Python
------------------------------------------------ By Ronit Singh.
What is List?
How to create list?
>>> a = [5,10,15,20,25]
>>> print(type(a))
<class 'list'>
>>> b = [5,10.2,30,"Ronit"]
>>> print(type(b))
<class 'list'>
>>> c = []
>>> print(type(c))
<class 'list'>
How to access list elements?
>>> print(a)
[5, 10, 15, 20, 25]
>>> print(a[0])
5
>>> b = [5,10.2,30,"Ronit"]
>>> print(b[3])
Ronit
Concept of Negative Indexing
List supports negative indexing. Generally list index starts from 0. But while using negatice indexing it starts from -1 and it print the elements from last index to first (reverse direction).
>>> print(b[-1])
Ronit
>>> print(b[-2])
30
Accessing list element via loop
>>> l1 = [50, 20, 80, 10, 60, 40]
>>> for i in l1:
... print(i,end=' ')
50 20 80 10 60 40
How to delete an element from list?
>>> l1 = [50, 20, 80, 10, 60, 40]
>>> del l1[2] # 80 is at index 2, it will be deleted
>>> print(l1)
[50, 20, 10, 60, 40]
How to edit an element in list?
>>> print(l1)
[50, 20, 10, 60, 40]
>>> l1[1] = 30 # 20 is at index 1, it will be now 30.
>>> print(l1)
[50, 30, 10, 60, 40]
>>> l1[5] = 70 # index 5 is not there in the list. It will give Error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
How to add new element in list?
There are two standard ways to add elements in the list.
append() and insert() : These are the attributes of list class.
append() : It adds the new element after the last index of the list.
syntax: append(element)
>>> l1 = [50,20,45,10,90,40]
>>> print(l1)
[50, 20, 45, 10, 90, 40]
>>> l1.append(70)
>>> print(l1)
[50, 20, 45, 10, 90, 40, 70]
insert() : It adds the new element at the specified index.
syntax: insert(index,element)
>>> l1.insert(3,100)
>>> print(l1)
[50, 20, 45, 100, 10, 90, 40, 70]
If the specified index is not present in the list then it will add the element at the last position.
>>> l1.insert(10,150) # index 10 is not present in the list.
>>> print(l1)
[50, 20, 45, 100, 10, 90, 40, 70, 150]
Packing and Unpacking
Example of Packing
>>> a=20
>>> b=30
>>> c=50
>>> l2 = [a,b,c]
>>> print(l2)
[20, 30, 50]
Example of Unpacking
>>> a,b,c=l2
>>> print(a,b,c)
20 30 50
Built-in Methods
>>> print(l1)
[50, 20, 45, 100, 10, 90, 40, 70, 150]
>>> len(l1) # returns the length of the list
9
>>> max(l1) # returns maximum data
150
>>> min(l1) # returns minimum data
10
>>> sum(l1) # returns sum of all data
575
>>> sorted(l1) # always returns a sorted list in ascending order.
[10, 20, 40, 45, 50, 70, 90, 100, 150]
>>> sorted(l1,reverse=True) # always returns a sorted list in descending order.
[150, 100, 90, 70, 50, 45, 40, 20, 10]
list() method
syntax : list(iterable)
>>> a = list()
>>> print(a)
[]
>>> b = list(10) #TypeError: 'int' object is not iterable
>>> c = list(10,20,30) #TypeError: list expected at most 1 argument, got 3
>>> d = list('Ronit') # 'string' object is a iterable
>>> print(d)
['R', 'o', 'n', 'i', 't']
>>> e = list(range(5))
>>> print(e)
[0, 1, 2, 3, 4]
>>> f = list([10,20])
>>> print(f)
[10, 20]
List Comparison
>>> a = [1,2,3]
>>> b = [2,3,1]
>>> c = [1,2,3,4,5]
>>> d = [1,2,3]
>>> a == d
True
>>> a > b
False
>>> c > a
True
List Concatenation
>>> l1 = [1,5,9]
>>> l2 = [2,3,1]
>>> l3 = l1 + l2
>>> print(l3)
[1, 5, 9, 2, 3, 1]
>>> l1 += l2
>>> print(l1)
[1, 5, 9, 2, 3, 1]
List Repetition
>>> l1 = [1,2]
>>> l1 = l1*3
>>> print(l1)
[1, 2, 1, 2, 1, 2]
List of Lists
>>> l1 = [[1,2,3],[4,5,6],[7,8,9]]
>>> print(l1)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> print(l1[0])
[1, 2, 3]
>>> print(l1[0][1])
2
>>> l1.append([10,11,12])
>>> print(l1)
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
List object method
>>> l1=[50,20,45,100,90,90,40,70,60,10]
>>> print(l1)
[50, 20, 45, 100, 90, 90, 40, 70, 60, 10]
insert(index,data)
>>> l1.insert(3,35)
>>> print(l1)
[50, 20, 45, 35, 100, 90, 90, 40, 70, 60, 10]
remove(data) : remove the first occurence of data. If data not present then it gives #ValueError.
>>> l1.remove(100)
>>> print(l1)
[50, 20, 45, 35, 90, 90, 40, 70, 60, 10]
>>> l1.remove(90)
>>> print(l1)
[50, 20, 45, 35, 90, 40, 70, 60, 10]
>>> l1.remove(100) #ValueError: list.remove(x): x not in list
pop() : removes and returns the last element
>>> x = l1.pop()
>>> print(x)
10
>>> print(l1)
[50, 20, 45, 35, 90, 40, 70, 60]
clear() : delete all elements of the list
>>> l2 = list('Ronit')
>>> print(l2)
['R', 'o', 'n', 'i', 't']
>>> l2.clear()
>>> print(l2)
[]
reverse() : reverse the list
>>> l1.reverse()
>>> print(l1)
[60, 70, 40, 90, 35, 45, 20, 50]
index(data) : return the index number of the specified data (first occurence). If data not present then it gives #ValueError.
>>> print(l1)
[60, 70, 40, 90, 35, 45, 20, 50]
>>> i = l1.index(35)
>>> print(i)
4
>>> i = l1.index(100) #ValueError: 100 is not in list
count(data) : returns the number of occurence of specified data.
>>> x = l1.count(70)
>>> print(x)
1
>>> x = l1.count(100)
>>> print(x)
0
sort() : sort the list in ascending order.
>>> print(l1)
[60, 70, 40, 90, 35, 45, 20, 50]
>>> l1.sort()
>>> print(l1)
[20, 35, 40, 45, 50, 60, 70, 90]
>>> l1.sort(reverse=True)
>>> print(l1)
[90, 70, 60, 50, 45, 40, 35, 20]
----------------------------- END --------------------------
Thank You..