0% found this document useful (0 votes)
8 views51 pages

Lecture 5

The document discusses lists in Python. It explains how to create, access, modify, sort and perform other operations on list elements. It also discusses lists of lists and nested lists.

Uploaded by

bsmt22098
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)
8 views51 pages

Lecture 5

The document discusses lists in Python. It explains how to create, access, modify, sort and perform other operations on list elements. It also discusses lists of lists and nested lists.

Uploaded by

bsmt22098
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/ 51

Lists

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

• Every item in a list has an index


• First item is at index 0
• Second item is at index 1
• 50th Item is at index 49
Index example
DIY
mylist = [5,7,9,11]

Element in mylist on index 0 is: 5


Element in mylist on index 1 is: 7
Element in mylist on index 2 is: 9
Element in mylist on index 3 is: 11
Accessing an element of a DIY
list

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)

Delete an element from 3rd index of the list.


Delete Elements
DIY
mylist = [2,4,6,8,10]
print(mylist)
mylist.pop(3)
print(mylist)

>>>
[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] )

From index To index

[2, 4, 5, 7, 9, 11, 12, 14]


[5, 7, 9]
>>>
Joining Two Lists
DIY
• There are 2 ways of doing that

• By using Extend method


• By using + operator
Joining Two Lists
DIY
a = [1,3,5,7]
b = [2,4,6,8]
a.extend(b)
print(a)

[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

mygrades = ["A", "B", "A-" , "B+", "C"]


mygrades.remove("B")
print(mygrades)

['A', 'A-', 'B+', 'C']


>>>
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)

[1, 2, 3, 5, 7, 12, 5, 4, 3, 2, 17]


>>>
Remove vs Pop
DIY
• Pop removes and return an item on an Index

• Remove will remove the first occurrence of an item


Find out the index of an item DIY

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)

[1, 2, 2, 3, 3, 4, 4, 5, 5, 7, 12, 17]


>>>
DIY
Sorting
a = ["Rashid","Ahmed","Zeeshan","Babar","Abdullah","Shahid"]
a.sort()
print(a)

['Abdullah', 'Ahmed', 'Babar', 'Rashid', 'Shahid', 'Zeeshan']


>>>
Reversing the order
DIY
mynums = [1,2,3,4,5,7,12,5,4,3,2,17]
print(mynums)
mynums.reverse()
print(mynums)

[1, 2, 3, 4, 5, 7, 12, 5, 4, 3, 2, 17]


[17, 2, 3, 4, 5, 12, 7, 5, 4, 3, 2, 1]
>>>
Maximum Value
DIY

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)

[1, 3, 4, 8, 43, 6, 3, 0, 33, 12, 3, 7, 9, 18]


[3, 5, 6, 10, 45, 8, 5, 2, 35, 14, 5, 9, 11, 20]
>>>
Elements of list
DIY
• Every element of list is an Object

mybools = [True, False, True, True, False]


mynums = [1,2,3,4,5,6]
myfloats = [1.0, 2.1, 3.2, 4.5 , 5.6]
mystrings =["Hello","how", "are","you"]
mymix = [3,5.7,"Hello",False]
Objects in a list
DIY
• Lists can contain integers
• Lists can contain floats
• Lists can contain boolean
• Lists can contain strings
• Lists can contain other objects
• Lists can contain …………….. Lists !
Lists of Lists
List in a List?
DIY
a = [1,2,3]
b = [4,5,6]
Superlist = [a,b]

print(Superlist)

[[1, 2, 3], [4, 5, 6]]


>>>
Example: List in a List?
DIY
a = [1,2,3]
b = [4,5,6]
c = [7,8,9]
Superlist = [a,b,c]

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


>>>
Imagine Output …
DIY
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>

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

Row 2 Row 2 Row 2


Col 1 Col 2 Col 3

Row 3 Row 3 Row 3


Col 1 Col 2 Col 3
Grid in python
• Grid rows index start from 0
• Grid column index starts from 0
Row 0 Row 0 Row 0
Col 0 Col 1 Col 2

Row 1 Row 1 Row 1


Col 0 Col 1 Col 2

Row 2 Row 2 Row 2


Col 0 Col 1 Col 2

You might also like