0% found this document useful (0 votes)
18 views88 pages

List

The document provides a comprehensive overview of Python lists, detailing their mutable nature, various methods for creation, and operations such as accessing, modifying, and removing elements. It explains functions like len(), append(), extend(), pop(), remove(), and sort(), along with examples of their usage. Additionally, it covers list slicing and the behavior of relational operators with lists.

Uploaded by

pyushr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views88 pages

List

The document provides a comprehensive overview of Python lists, detailing their mutable nature, various methods for creation, and operations such as accessing, modifying, and removing elements. It explains functions like len(), append(), extend(), pop(), remove(), and sort(), along with examples of their usage. Additionally, it covers list slicing and the behavior of relational operators with lists.

Uploaded by

pyushr013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 88

LIST

List is a mutable data type.


The values inside the list can be changed.
List can be a combination of values of same data
type or it can be values of different data type.
Len() function is used to calculate the length of
the list.
Id attribute is used to find out the address of the
list.
LIST
List can be created in many ways.
1. L=[] will create an empty list.
print(l)
L=LIST() will create an empty list
2. l=[10,20,"aman",12.5]
print(l)
Output:-
[]
[10, 20, 'aman', 12.5]
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

[11, 22, 33, 44, 55, 66, 77]


[33, 44, 55]
[33, 44, 100, 300]
LIST
1. len() is used for calculating the length of
the list.
L=[1,2,3,4,5]
C=len(L)
print©
Output
5
LIST
2. append()-This function takes only one parameter and
appends the value which is provided as parameters to the
list.

If at all two values have to passed then it should be in


square brackets which will be appended as a single value
to the list.

Irrespective of number of values in square brackets the


length will be incremented only by one.
LIST
Li=[10,20,30]
l2=li.append(90)#non returning
print("after appending")
print(l2)
print(li)
l=len(li)
print("length of the list=",l)
#li.append(90,100)
li.append([99,100])
print("calculating length by using append and sending 2 values in single subscript")
l=len(li)
print(li)
print(l)
LIST
Output:-
after appending
None
[10, 20, 30, 90]
length of the list= 4
calculating length by using append and sending 2
values in single subscript
[10, 20, 30, 90, [99, 100]]
5
LIST

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.

The resultant list of both append and extend function can


not be assigned to the new list. Even it is assigned it will
return none.

If more than one value to be appended to the existing list as


a separate values then extend function should be used.
LIST
Li=[10,20,30]
li.extend([111,222])
print("calculating length by using extend and
sending 2 values in single subscript")
l=len(li)
print(li)
print(l)
LIST
OUTPUT:-
calculating length by using extend and
sending 2 values in single subscript
[10, 20, 30, 111, 222]
5
LIST

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.

If the index of value which is existing is passed then the function


will pop an element which is present in the index which is passed
as a parameter.
LIST
li=[10,20,30,40,11,22,5,13]
print(li)
l=len(li)
print("length of the list",l)
c=li.pop()
print("the poped element is",c)
l=len(li)
print("length of the list",l)
LIST
OUTPUT:-

[10, 20, 30, 40, 11, 22, 5, 13]


length of the list 8
the popped element is 13
length of the list 7
LIST
OUTPUT:-

[10, 20, 30, 40, 11, 22, 5, 13]


length of the list 8
the popped element is 13
length of the list 7
LIST
OUTPUT:-

[10, 20, 30, 40, 11, 22, 5, 13]


length of the list 8
the popped element is 13
length of the list 7
LIST

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.

If the value which is passed as a parameter is


not existing then interpreter will throw an
error.
LIST
l=[1,2,3,4,5,6]
c=len(l)
print("length of the list",c)
l.remove(5)
print(l)
c=len(l)
print("length of the list",c)
l.remove(15)
LIST
OUTPUT:-
length of the list 6
[1, 2, 3, 4, 6]
length of the list 5
l.remove(15)
ValueError: list.remove(x): x not in list
LIST
6. Del() function deletes all the values and also deletes
the list forever.
li=['a','b','c','d','e']
print(li)
print("after using del function")
del li
print(li)
l=len(li)
print("length=",l)
LIST
Output:-
['a', 'b', 'c', 'd', 'e']
after using del function
print(li)
NameError: name 'li' is not defined
LIST
7. Clear() function clears all the values and
empty list is still existing.
li=[10,20,30,40]
print("after using clear function")
li.clear()
print(li)
l=len(li)
LIST
Output:-

after using clear function


[]
LIST
8. Count() counts how many times the passed value is existing in the list. The
value is not existing then it will return 0.

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 updating value can be a –ve or a positive value.

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, 20, 30, 40, 50, 60, 70, 80, 90]


[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[10, 40, 70, 100]
[]
[]
[100, 90, 80, 70, 60, 50]
[40, 30]
[100, 80, 60, 40, 20]
LIST
li=[10,20,30,40,50,60,70,80,90,100]
print(li[-4:-7:-1])
print(li[-4:-9:-2])
print(li[:-9:-1])
print(li[0:9:-1])
print(li[0: :-1])
print(li[-4:-7:1])
print(li[-4:-9:2])
print(li[:-9:-1])
LIST
OUTPUT
[70, 60, 50]
[70, 50, 30]
[100, 90, 80, 70, 60, 50, 40, 30]
[]
[10]
[]
[]
[100, 90, 80, 70, 60, 50, 40, 30]
LIST
li=[10,20,30,40,50,60,70,80,90,100]
print(li[4:7:-1])
print(li[4:9:-2])
print(li[:9:-1])
print(li[0:9 :-1])
print(li[0: :-3])
print(li[7: :-1])
print(li[ :4:-1])
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)]

l=[x**2 for x in range(5)]


print(l)
Output:-
[0, 1, 4, 9, 16]
LIST
z=2
L = [[ z for x in range(0,10)] for y in range(5,10)]
print(L)
Output:-
[[2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2, 2, 2,
2, 2], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [2, 2, 2, 2, 2, 2,
2, 2, 2, 2], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]
LIST
l=[x for x in range(4,8) for y in range(3)]
print(l)

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

l=[0 for i in range(5)]


print(l)

l=[[j for j in range(5)]for i in range(5)if i


%2==0]
print(l)
LIST
output
[0, 0, 0, 0, 0]
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
LIST

If a list is created by assigning another list by


using “=“ operator all the manipulations like
appending, deleting, popping, changing the
values in any one of the list variables are
reflected in another variable also as these
both lists are referring to the same location.
LIST
l1=[10,20,30,40,50,60,[90,100]]
l2=l1
print(l1)
print(l2)
print("after changing the value")
l1[6][1]=1000
print(l1)
print(l2)
LIST
OUTPUT
[10, 20, 30, 40, 50, 60, [90, 100]]
[10, 20, 30, 40, 50, 60, [90, 100]]
after changing the value
[10, 20, 30, 40, 50, 60, [90, 1000]]
[10, 20, 30, 40, 50, 60, [90, 1000]]
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.

• But in the case of nested list , the changes will


be reflected in both, if change made to any
one the nested list present in the list.
LIST

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

If one list object is created by using the copy () function of


copy module and passing the existing list as a parameter,
the changes made to any one of the lists will not be
reflected in another list as they refer to different locations.

If the list contains nested list the changes made to nested


list of the any one of the list, then those changes will be
reflected in the nested list of another list also.

This copy that is created is knows as shallow copy.


LIST
import copy
l1=[[10,20],[30,40],[50,60]]
l2=copy.copy(l1)
print(l1)
print(l2)
print(id(l1))
print(id(l2))
l1.append([70,80])
print(l1)
print(l2)
l1[0][0]=100
print(l1)
print(l2)
l1.insert(0,5)
print(l1)
print(l2)
l2.pop()
print(l1)
print(l2)
LIST
OUTPUT
[[10, 20], [30, 40], [50, 60]]
[[10, 20], [30, 40], [50, 60]]
1607881576832
1607881572096
[[10, 20], [30, 40], [50, 60], [70, 80]]
[[10, 20], [30, 40], [50, 60]]
[[100, 20], [30, 40], [50, 60], [70, 80]]
[[100, 20], [30, 40], [50, 60]]
[5, [100, 20], [30, 40], [50, 60], [70, 80]]
[[100, 20], [30, 40], [50, 60]]
[5, [100, 20], [30, 40], [50, 60], [70, 80]]
[[100, 20], [30, 40]]
LIST
If one list object is created by using the deepcopy () function
of copy module and passing the existing list as a parameter,
the changes made to any one of the lists will not be
reflected in another list as they refer to different locations.

If the list contains nested list the changes made to nested


list of the any one of the list, then those changes will not be
reflected in the nested list of another list also.

This copy that is created is knows as deep copy.


LIST
l1=[[10,20],[30,40],[50,60]]
l2=copy.deepcopy(l1)
print(l1)
print(l2)
print(id(l1))
print(id(l2))
l1.append([70,80])
print(l1)
print(l2)
l1[0][0]=1000
print(l1)
print(l2)
LIST
OUTPUT
[[10, 20], [30, 40], [50, 60]]
[[10, 20], [30, 40], [50, 60]]
2395296813632
2395296808896
[[10, 20], [30, 40], [50, 60], [70, 80]]
[[10, 20], [30, 40], [50, 60]]
[[1000, 20], [30, 40], [50, 60], [70, 80]]
[[10, 20], [30, 40], [50, 60]]

You might also like