Professional Documents
Culture Documents
Week11 12 Lists Fromsheeba
Week11 12 Lists Fromsheeba
Lecture 11 & 12
Lists
Lists
Topics to be covered today.
• Introduction to Arrays and Lists
• Using arrays and lists to store homogeneous data
elements.
• Predefined functions for lists
• Dynamic Lists
• Simple operations on lists
Sum and product
Maximum and Minimum
• Lists as parameters to functions.
• Multi-dimensional Lists and Matrix operations.
List and Arrays – A gentle Introduction
Lets assume we had to store 5 integers:
− One option would be to use five variables and store the
integers therein; If the integers were 5,2,-5,2,43, we
could have:
A=5
B=2
C=-5
D=2
E=43
The above option would be operational but it is not
a very efficient approach, because manipulating
the variables would be a tedious task.
Arrays and Lists
Most if not all programming languages allow homogenous
data (i.e. Data of the same type) to be stored in simple data
structures. In some Programming Languages, these data
structures are called arrays, in Python, they are called
Lists.
myList=[5,2,-5,2, 43]
To print the above list, we could have simply written
print myList
The output would be:
− [5,2,-5,2, 43]
If we wanted to display the elements individually on
a separate line, we could have used the codes
below:
for i in range(5):
print myList[i] #i being used as the index
Manipulating List Elements
Since it is possible to access the individual
elements, we can also manipulate these elements.
Let's assume we wanted to display the square of all
the elements in a list.
myList=[5,2,-5,2, 43]
for i in range(5):
a= myList[i] #i is the index
b=a*a
print b
Adding up two homogenous lists
listA=[1,5,3,2,3,1,3]
listB=[78,8,7,8,45,3,5]
for i in range(7):
a=listA[i] #indexing the elements of listA
b=listB[i] #indexing the elements of listB
c=a+b #Adding each element
print c
---------------------------------------------------------
We can also use some pre-defined functions
with list, these functions include len, sort...
List operations
A=[1,34,43.......]
B=[23,34,34.....]
sum=sumList(A,B)
print sum
------------------------------------------------------------
Notice that we can pass lists as parameters to
functions, and that functions can also return lists
Program to find the Maximum value in a list
The Algorithm is:
Step 1.
Assume the first element is the biggest, i.e. assign Max to the first element,
Step 2.
If we have not yet reached the end of the list, Proceed, else jump to Step 5
Step 3.
Compare Max with the current element in the current Slot.
If the element in the current slot is greater than Max, Do Step 4
Step 4.
Assign the value in the current slot to Max
Step 5.
Current jumps to the next slot, increment i, repeat Step 2
Step 6.
Print the Maximum element
The Codes
A=[1,5,8,44,8,4,5,45]
Max=A[0] # Step 1
I=1
while I<len(A): #Step 2
if A[I]>Max: #Step 3
Max=A[I] #Step 4
I=I+1 #Step 5
print “The biggest value in List ”,A,” is ”, Max
#Step 6
Dry running the Codes!
I=0
Max=A[i]=A[0]=1 len(A)=8
I=1
A I I<len(A) A[I] Max A[I]>Max Max
[1,5,8,44,8,4,50,45] 1 No 5 1 Yes 5
[1,5,8,44,8,4,50,45] 2 No 8 5 Yes 8
[1,5,8,44,8,4,50,45] 3 No 44 8 Yes 44
[1,5,8,44,8,4,50,45] 4 No 8 44 No 44
[1,5,8,44,8,4,50,45] 5 No 4 44 No 44
[1,5,8,44,8,4,50,45] 6 No 50 44 Yes 50
[1,5,8,44,8,4,50,45] 7 No 45 50 No 50
8 Yes
Lists – Passed by reference
def changeList(listA):
listA[0]=3
A=[1,34,43]
changeList(A)
print A
---------------------------------------------------------------
If we try to change individual values of a list, the
changes are reflected where the function is called
Lists are mutable, unlike strings
Python Lists v/s Conventional Arrays
There are some basic differences between Python Lists
and Conventional Arrays.
− The basic difference is that Python Lists are dynamic, i.e
they can grow or shrink using (append and remove
methods)
− Lists in Python also support heterogenous types.
Conventional arrays usually store multiple data elements of
the same type, whereas in Python we can have a list which
stores data of several types, e.g
− myHetList=[1,2.31,'a',”well”], however special care must be
taken while manipulating them
Multi-Dimensional Lists
So far, we have only considered one-dimensional
lists (list of a certain number of rows), it is also
possible to have multi-dimensional lists, i.e. made up
of n rows by m columns.
Do populate such a list, it is very easy and we can
proceed using the following algorithm:
For each row, populate each column, this gives us the
following implementation:
Populating a multi-dimensional List
num_Rows=input(“Enter number of rows”)
num_Columns=input(“Enter number of Columns”)
my2dList=[]
for i in range(num_Rows):
Cols=[]
for j in range(num_|Columns):
data=input(“Enter Data”)
Cols.append(data)
my2dList.append(Cols)
The Codes - Explained!
my2dList=[]
for i in range(rows):
for j in range (cols)):
MatrixC[i][j]=MatrixA[i][j]+MatrixB[i][j]
Printing the results
#printing MatrixA
for i in range(rows):
for j in range (cols):
print MatrixA[i][j],
print
#printing MatrixB
for k in range(rows):
for l in range (cols):
print MatrixB[k][l],
print
#printing MatrixC
for m in range(rows):
for n in range (cols):
print MatrixC[m][n],
print
Avoiding Repetition (Once Again!)
We can see from the previous slide that we do
have repetition once again, we could use a
function to display, whereby each array could be
passed as parameter, along with the number of
rows and number of columns.
For the Multiplication of Two Matrices A and B
to be defined, the two matrices must be
conformant, i.e. The number of Columns in the
first one should be equal to the number of Rows
in the second one.
For this question, simply assume that both
matrices are of the same order, i.e they have the
same number of Rows and Columns
The Solution
for i in range(num_Rows):
for j in range (num_Columns):
for k in range(num_Rows):
MatrixC[i][j]+=MatrixA[i][k]*MatrixB[k][j]