Professional Documents
Culture Documents
Lecture 5
Lecture 5
Lists
More than just objects
• Usually we are not dealing with a few objects
• We can have a long list of numbers
• A list of shopping items
• I list of names of students
• A list of vehicles that did not pay tax
……… what? List?
Yes List
• Think of a few lists
Lists in Python
Lists in Python
• Lists in python are simple
• As usual, no detail related to memory allocation shared
• Just put the elements in the list of any type
• Each list consists of objects
Creating a List
DIY
• mylist = [1,2,3,4]
Creating an empty list
DIY
mylist = []
Printing the whole list
DIY
mylist = [1,2,3,4]
print(mylist)
List index start at 0
DIY
mylist = [1,2,3,4]
print(mylist[2])
>>> 3
Accessing Elements: Example DIY
nameList = ["Tom", "Dick", "Harry"]
print( nameList[1])
print( nameList[0])
print( nameList[2])
>>>
Dick
Tom
Harry
Changing Element of a List
DIY
mylist = [1,2,3,4]
mylist[0] = 25
Changing Elements
DIY
mylist = [1,2,3,4]
print(mylist)
mylist[0] = 25
mylist[3] = 0
print(mylist)
>>>
[1, 2, 3, 4]
[25, 2, 3, 0]
Assigning one element DIY
of a List to another
mylist = [2,4,6,8,10,12,14,16,18,20,22]
print(mylist)
mylist[0] = mylist[5]
mylist[6] = mylist[3]
print(mylist)
>>>
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22]
[12, 4, 6, 8, 10, 12, 8, 16, 18, 20, 22]
Checking length of list
DIY
mylist = [1,2,3,4]
mylist2 = [2,4,6,8,10,12,14,16,18,20,22]
print( len(mylist) )
print( len(mylist2) )
Adding elements later (at the end)
DIY
mylist = [2,4,6,8,10]
print(mylist)
mylist.append(55)
mylist.append(66)
print(mylist)
>>>
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10, 55, 66]
Delete Elements
DIY
mylist.pop(3)
>>>
[2, 4, 6, 8, 10]
[2, 4, 6, 10]
Insert an element
DIY
mylist = [2,4,6,8,10]
print(mylist)
mylist.insert(3, 20)
print(mylist)
>>>
[2, 4, 6, 8, 10]
[2, 4, 6, 20, 8, 10]
List of objects
• Please remember, every List is a list of objects
• Because everything we represent in Python is an object
• This means a list can have multiple type of data
y=9
F = [2, 3.7, False, "Yes", True, -99.32, y]
Getting a sub-list
DIY
thelist = [2,4,5,7,9,11,12,14]
print( thelist )
print( thelist[2:5] )
[1, 3, 5, 7, 2, 4, 6, 8]
>>>
Joining Two Lists
DIY
a = [1,3,5,7]
b = [2,4,6,8]
a= a+b
print(a)
[1, 3, 5, 7, 2, 4, 6, 8]
>>>
Removing a particular item
DIY
• Remove method
mynums = [1,2,3,4,5,7,12,5,4,3,2,17]
mynums.remove(4)
print(mynums)
mynums = [1,2,3,4,5,7,12,5,4,3,2,17]
print(mynums.index(5))
4
>>>
Count the occurrences of an element
DIY
mynums = [1,2,3,4,5,7,12,5,4,3,2,17]
print(mynums.count(5))
2
>>>
Sorting Lists
DIY
• Sort()
• Function Sort can be called only if all elements are of same type
Sorting
DIY
mynums = [1,2,3,4,5,7,12,5,4,3,2,17]
mynums.sort()
print(mynums)
a = [1,2,3,4,5,7,12,5,4,3,2,17]
print(max(a))
17
>>>
Maximum Value
DIY
a = ["Rashid","Ahmed","Zeeshan","Babar"]
print(max(a))
Zeeshan
>>>
Minimum Value
DIY
a = [1,2,3,4,5,7,12,5,4,3,2,17]
print(min(a))
1
>>>
Minimum Value
DIY
a = ["Rashid","Ahmed","Zeeshan","Babar"]
print(min(a))
Ahmed
>>>
List Example: Incrementing
DIY
• Incrementing everything in a list by 2
thelist = [1,3,4,8,43,6,3,0,33,12,3,7,9,18]
print(thelist)
for i in range(0,len(thelist)):
thelist[i] +=2
print(thelist)
print(Superlist)
or
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
As a grid!
Grid
• Grid has some rows
• Grid has some columns
Row 1 Row 1 Row 1
Col 1 Col 2 Col 3