List
List
List can be created by using list function passing existing list as parameter.
3. l1=list(l)
print(l1)
st="well done"
4. l2=list(st)
print(l2)
Output
[10, 20, 'aman', 12.5]
['w', 'e', 'l', 'l', ' ', 'd', 'o', 'n', 'e']
LIST
5. List can be created by combining two lists
in the following manner:
l3=l1+l2
print(l3)
Output:-
[10, 20, 'aman', 12.5, 'w', 'e', 'l', 'l', ' ', 'd', 'o',
'n', 'e']
LIST
6. List can also be created from the existing
list in the following manner:
l1=[10,20,30,40,50,60]
l2=l1[2:5]
print(l2)
Output:
[30, 40, 50]
Note:-L1[2:5]-will extract values from index 2
to 4(1 less than final index i.e 5)
LIST
7. List can be created with the repeated values
in the following manner.
l1=[10,20,30,40,50,60]
l3=l1*3
print(l3)
Output:-
[10, 20, 30, 40, 50, 60, 10, 20, 30, 40, 50, 60, 10,
20, 30, 40, 50, 60]
LIST
8. list can be created by accepting values in the following manner using
input function.
l=list(input("enter"))
print(l)
9. It can be created using eval() function also.
l=eval(input("enter"))
print(l)
OUTPUT:-
enter1234567
['1', '2', '3', '4', '5', '6', '7']
enter[10,20,30,40,50]
[10, 20, 30, 40, 50]
LIST
10. List can also be created in the following
manner
n=int(input("enter the number"))
l=[[0] for i in range(n)]
for i in range(n):
l[i]=int(input("Accept the number"))
print(l)
by deciding its size at run time.
LIST
enter the number5
Accept the number111
Accept the number222
Accept the number333
Accept the number444
Accept the number555
[111, 222, 333, 444, 555]
LIST
List can not be created by passing int object
as a parameter as it is not iterable.
The following statement will throw an error.
No=1234
L=list(no)
Print(L)
LIST
Individual elements of a list can be accessed in the
following manner.
L=[1,2,3,4,5]
print(l[3])
As list is mutable data types its values can be updated
L[0]=l[0]+10
Print(l[0])
Output-4 11
LIST
‘in’ and ‘not in’ operators can be used with list.
L=[1,2,3,4,5]
print (2 in li)
print(6 not in li)
Output:-
True
True
LIST
The use of relational operators on lists.
list1, list2=[2,3,4],[2,3,4]
list3=[10,20,30]
print (list1==list2)
print(list1!=list2)
print(list1==list3)
print(list1<=list2)
print(list3>list1)
print(list1!=list2)
LIST
Output:-
True
False
False
True
True
False
LIST
List can be accessed both in forward
direction and in backward direction as we
have seen in strings.
List index always starts with 0 and starts with
-1 in backward direction.
-6 -5 -4 -3 -2 -1
10 20 30 40 50 60
0 1 2 3 4 5
LIST
The values of the list can be accessed both in
forward and backward direction.
l=[10,20,30,40,"aman",1.5,"A"]
print(l[0:5])
print(l[1:20:2])
print(l[-1:-9:-1])
print(l[-1:-25:-1])
print(l[-1:-8:1])
LIST
Output
[10, 20, 30, 40, 'aman']
[20, 40, 1.5]
['A', 1.5, 'aman', 40, 30, 20, 10]
['A', 1.5, 'aman', 40, 30, 20, 10]
[]
LIST
One can assign the slice of the list to create a
new list.
l=[11,22,33,44,55,66,77]
l1=l[2:5]
print(l)
print(l1)
l1[2:4]=[100,300]
print(l1)
LIST
Output
3. extend() function will not take more than 1 argument. If we have pass more
than one value in a square bracket they will be appended to the list as separate
values and the length incremented as per the number of values you are passing
as a parameter.
4. Pop() functions pops last element and the popped element can
be trapped as it returns the popped element.
Pop() function taking the index which is out of range will throw
an error.
l=[1,2,3,4,5,6]
c=l.pop(2)
print(c)
print(l)
LIST
Output:-
3
[1, 2, 4, 5, 6]
LIST
5. Remove() takes one parameter removes
the parameter from the list.
li=[111,222,333,444,555,111]
print(li)
print(li.count(111))
c=li.count(111)
print(c)
Output:-
[111, 222, 333, 444, 555, 111]
2
2
LIST
9.Insert() inserts the value which is passed as second parameter in list in the position
which is passed as a parameter.
li=[111,222,333,444,555,111]
li.insert(4,666)
print(li)
print(li.index(666))
OUTPUT:-
[111, 222, 333, 444, 666, 555, 111]
4
LIST
10. Index() gives the index of the value which is
passed as a parameter. If value is not present it
will throw an error.
li=[111,222,333,444,555,111]
print(li.index(444))
c=li.index(444)
print(c)
print(li.index(66))
c=li.index(66)
print(c)
LIST
Output:-
3
3
print(li.index(66))
ValueError: 66 is not in list
LIST
11. Sort()-sorts all the values and arrange them in
the ascending order
If in the sort function we write reverse=True then
list will be sorted in the descending order.
Sort() function will work provided the values in the
list are of the same type. If values stored in the list
are of different data type then it will throw a type
error.
LIST
li=[111,232,333,144,555,111]
li.sort()
print(li)
Output:
[111, 111, 144, 232, 333, 555]
LIST
li=[111,232,333,144,555,111]
li.sort(reverse=True)
print(li)
Output:-
[555, 333, 232, 144, 111, 111]
LIST
In list slices there are there values. The first one stands for initial from where
the journey starts The second value stands for the destination. The third value
stands for updating.
The user should consider whether the journey through the list starting from
initial value to the destination value using updating is possible or not.
The slice of the existing list can be updated with new values.
LIST
Some list slices
li=[10,20,30,40,50,60,70,80,90,100]
print(li[:9:1])
print(li[0: :1])
print(li[0: :3])
print(li[9:3:1])
print(li[3:1:1])
print(li[9:3:-1])
print(li[3:1:-1])
print(li[9:0:-2])
LIST
OUTPUT:-
[]
[]
[]
[10]
[80, 70, 60, 50, 40, 30, 20, 10]
[100, 90, 80, 70, 60]
LIST
list1=[111,222,333,444,[440,441],555,'a',"rudali",666]
print(list1[:3:]+list1[3:])
print(list1[-3:]+list1[:-3])
print(list1[-4:8:1])
print(list1[-4:6:-1])
print(list1[-1:-4:-1])
print(list1[-1:-4:1])
LIST
OUTPUT
[111, 222, 333, 444, [440, 441], 555, 'a', 'rudali', 666]
['a', 'rudali', 666, 111, 222, 333, 444, [440, 441], 555]
[555, 'a', 'rudali']
[]
[666, 'rudali', 'a']
[]
LIST
l=[10,20,30,40,50,60,70,80,90,100]
print(l[0: :-1])
print(l[0:-11:-1])
print(l[0:-9:-1])
print(l[5:-6:-1])
print(l[5:5:1])
print(l[-5:5:1])
print(l[6:-5:-1])
print(l[-6:-5:-1])
LIST
OUTPUT
[10]
[10]
[]
[60]
[]
[]
[70]
[]
LIST COMPREHENSION
List comprehension is an elegant way to
define and create lists based on existing lists.
matrix=[[ j for j in range(5)] for i in range(5)]
print(matrix)
Output:-
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0,
1, 2, 3, 4], [0, 1, 2, 3, 4]]
LIST
L = [[x**2 for x in range(0,10)] for x in
range(5)]
Output:-
[4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7]
LIST
st=input("enter the string")
L = [x*3 for x in st]
print(L)
l = [[x*3 for y in range(3)] for x in st]
print(l)
Output:-
enter the stringgod
['ggg', 'ooo', 'ddd']
[['ggg', 'ggg', 'ggg'], ['ooo', 'ooo', 'ooo'], ['ddd', 'ddd', 'ddd']]
LIST
vec = [-4, -2, 0, 2, 4]
L = [abs(x) for x in vec]
print(L)
Output:-
[4, 2, 0, 2, 4]
LIST
L = [(x, x**2) for x in range(4)]
print(L)
Output:-
[(0, 0), (1, 1), (2, 4), (3, 9)]
LIST
vector = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
L = [number for list in vector for number in
list]
print(L)
Output:-
[1, 2, 3, 4, 5, 6, 7, 8, 9]
LIST
l=[[1,2,3],[4,5,6]]
L=[[i for j in i ]for i in l]
print(L)
L=[[j for j in i ]for i in l]
print(L)
Output:-
[[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6], [4, 5,
6]]]
[[1, 2, 3], [4, 5, 6]]
LIST
n=int(input("Accept the limit"))
matrix=[[ j for j in range(n)] for i in range(n)]
print(matrix)
Output:-
Accept the limit3
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
LIST
print("after appending")
l1.append(70)
print(l1)
print(l2)
print("after inserting")
l2.insert(0,5)
print(l1)
print(l2)
LIST
after appending
[10, 20, 30, 40, 50, 60, [90, 1000], 70]
[10, 20, 30, 40, 50, 60, [90, 1000], 70]
after inserting
[5, 10, 20, 30, 40, 50, 60, [90, 1000], 70]
[5, 10, 20, 30, 40, 50, 60, [90, 1000], 70]
LIST
print("after poping")
l1.pop()
print(l1)
print(l2)
print(id(l1))
print(id(l2))
LIST
OUTPUT
after poping
[5, 10, 20, 30, 40, 50, 60, [90, 1000]]
[5, 10, 20, 30, 40, 50, 60, [90, 1000]]
2937596305856
2937596305856
• If a list is created by using copy() function as
shown in the example the changes made to
one list will not reflect in another list as they
refer to different locations.
l1=[10,20,30,40,50,60,[70,80]]
print(l1)
l2=l1.copy()
print(l2)
print(id(l1))
print(id(l2))
l1[0]=100
print(l1)
print(l2)
LIST
OUTPUT
[10, 20, 30, 40, 50, 60, [70, 80]]
[10, 20, 30, 40, 50, 60, [70, 80]]
2825848578496
2825854301184
[100, 20, 30, 40, 50, 60, [70, 80]]
[10, 20, 30, 40, 50, 60, [70, 80]]
LIST
l1[6][-1]=800
print(l1)
print(l2)
print(id(l1[6]))
print(id(l2[6]))
l2[6][-1]=8000
print(l1)
print(l2)
LIST
OUTPUT
[100, 20, 30, 40, 50, 60, [70, 800]]
[10, 20, 30, 40, 50, 60, [70, 800]]
2825854299712
2825854299712
[100, 20, 30, 40, 50, 60, [70, 8000]]
[10, 20, 30, 40, 50, 60, [70, 8000]]
LIST