Lists in Python
The containers
To store a list of values
Of different data types (string, int, float, list)
Has ordered sequence
Elements are enclosed in [square
bracket]
Elements are separated by commas(,)
1
Lists in Python
•Similar to strings but differs in mutability of the
two
•Are mutable i.e. elements are changeable
String List
Name=“PYTHON” name=[‘P’,’Y’,’T’,’H’,’O’,’N’]
Name[0]=‘M’ name[0]=‘M’
2
Lists in Python
To store a list of values
name=[‘P’,’Y’,’T’,’H’,’O’,’N’]
name name of list
P Y T H O N
0 1 2 3 4 5 index no
3
Lists in Python
Of mixed data type
[1,2,3,4,5]
[1.0,2.0,3.0,4.0]
[‘P’,2,3.0,]
4
Creating List
Note: Square brackets must be used
Empty List
Name=list()
Long Lists
li1=[1,2,3,4,5,6,7,8,9]
Nested lists
li1= [1,2,[2.1,2.2], 4]
5
Creating List (contd..)
From existing sequences
St=“hello”
li1=list(St)
or
li1=list(‘hello’)
L2=L1
L3=L1[from:till] #using list slice
L4=L1[:]
6
Creating List
Accepting from user
L1=list(input(“Enter the elements: ”))
This is mandatory
Note: All the inputs whether or not are treated as
string
eval()
To evaluate and return given expression
or to identify the given expression
eval(‘5+10’) o/p: 15
L1=eval(input(“Enter the elements: ”))
7
Traversing a List
backward indexing (starts from -1)
forward indexing (starts from 0)
Similarity with strings
Has functions like
Len, indexing & slicing, Membership
operators (in, not in), concatenation(+)
and replication(*)
8
Program to print elements of a list
[‘p’,’y’,’t’,’h’,’o’,’n’] with element’s
both indexes positive & negative
L= [‘p’,’y’,’t’,’h’,’o’,’n’]
Length=len(L)
for a in range(Length):
print("L[",a,"] and L[",a-Length,”] “,L[a])
9
Equality (Comparing Lists)
L1=[1,2,3]
L2=[1,2,3]
L3=[1,[2,3]]
L1<L2 o/p: False
L1<L3 o/p: Error
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
L1<L3
TypeError: unorderable types: int() < list()
Reason: L1[1] is integer whereas L3[1] is a list,
so these are not of comparable types.
10
Comparing Lists
[1,2,3,4] < [1,2] False First List is bigger than the
second
[1,2] < [1,2,3,4] True First List is smaller than the
second
[1,2,3,4]<[1,2,4,3] True Corresponding third element is
smaller in the first list
[1,2]==['1','2'] False Values of list must be same
[1,2] == [1.0,2.0] True D. type is ignored & values matters
11
List operations
Joining lists
l1=[1,2,3] l2=[4,5,6] l1+l2
Replicating lists
l1*3
Slicing lists
l1=[‘p’,’y’,’t’,’h’,’o’,’n’,]
l1[1:4] o/p: y, t, h
l1[1:-2] o/p: y, t, h
l1[-15:-1] o/p: p, y, t, h, o, n
Note: Giving limit much lower/upper, but python
returns from list falling in the range
12
List Modification using slices
L=['h','e',‘l','l'] L[3:]=“real”
print(L) L
o/p: ['h', 'e', 'l', 'l'] o/p: ['d', 'e', 'l', 'r', 'e', 'a', 'l']
L[0:1]=['d','w'] Note: string is a seq.
print(L) L[3:]=123
o/p: ['d', 'w', 'e', 'l', 'l'] o/p: error
L[0:2]=['d'] TypeError: can only
assign an iterable
print(L)
o/p: ['d', 'e', 'l', 'l']
L[10:15]=“hell”
L
Note: Only sequence to be
assigned o/p: ['d', 'e', 'l', 'l', 'r', 'e',
'a', 'l', 'h', 'e', 'l', 'l']
13
Working with List
append()
pop()
L1=[1,2,3]
L1=[1,2,3]
L1.append(4)
L1.pop(2) o/p: 2
L1 o/p: 1,2,3,4
L1.pop() o/p: 3
Updating elements
L1[2]=13
L1
o/p: 1,2,13
Deleting elements
del L1[3]
Note: del can remove a list of
or slice as well but pop() can
del L1[1:3] #to delete in remove only single element
slices but no slice list. pop() returns
del L1 #to delete all the deleted elements
at once
14
List Function and Methods
1. index() 2. extend()
L1=[1,2,3,4,5,6,7,8] L1=[‘a’, ‘b’, ‘c’]
L1.index(5) o/p: 4 L2=[‘d’,’e’]
L1.index(10) L1.extend(L2)
o/p: o/p: [‘a’, ‘b’, ‘c’, ‘d’, ’e’]
ExceptionValueError: 10 is
not in list
Note: extend() can add
multiple elements &
append() adds a single
element
15
List Function and Methods (contd..)
3. Insert() 4. remove()
To insert elements To remove element
anywhere in the list. from the list
L1=[1,2,3,4,5] L1. remove(2)
L1.insert(2,2.3)
o/p: [1, 2, 2.3, 3, 4, 5]
16
List Function and Methods (contd..)
5. clear() 6. count()
Removes all the items Returns count of the
from the list number of item in
the list
L1=[1,2,3,4,5] L1=[1,2,3,4,5,1]
L1.clear() L1.count(1)
L1
o/p: [ ] o/p: 2
17
List Function and Methods (contd..)
7. reverse()
To reverse the data in the list
L1=[1,2,3]
L1.reverse() o/p: [3,2,1]
8. sort()
To sort the elements of list, default
ascending order
L1.sort()
L1.sort(reverse=True) #For sorting in
descending
18
List Function and Methods (contd..)
9. sorted()
Returns the sorted element in the list.
Sorted(l1)
19
Lists Tuples Set
ordered ordered unordered
and and and
changeable unchangea unindexed
ble
Allows Allows No
duplicate duplicate duplicate
members members member
[] () {}
20
Thank you
21