And now for something completely different . . . 28-Apr-19 AHD c 2010
Part 7 Lists 28-Apr-19 AHD c 2010
Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010
Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010
Introduction to Lists Most real-world Python programs contain lists. Lists allow you to collect objects, so that you can treat them as a group. Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010
Introduction to Lists Most real-world Python programs contain lists. Lists allow you to collect objects, so that you can treat them as a group. Lists have left-to-right positional ordering, with index capability. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. 28-Apr-19 AHD c 2010
Lists are arrays of object references Technically, Python lists contain zero or more references to other objects. Lists do not contain the actual objects, just a reference to (address of) them. (Aside for C and C++ programmers: Python lists are essentially arrays of pointers. They are implemented as C arrays inside the Python interpreter. Fetching an item from a Python list is about as fast as indexing a C array, i.e. fast.) 28-Apr-19 AHD c 2010
A variable (or simple variable) can take a single value. A list variable is a special kind of object that can take many values - and they don't need to be all of the same data type. 28-Apr-19 AHD c 2010
Say you want to store the exam results of 30 students Say you want to store the exam results of 30 students. You could create 30 variables: result1 = 67 result2 = 95 result3 = 58 and so on . . . 28-Apr-19 AHD c 2010
If you only had a few results to score, this method would be acceptable. But what if you had to store the results of each student in the college, not just one class? 28-Apr-19 AHD c 2010
Storing 2000 results in this way would be a very tedious and error-prone process. You would have to have 2000 different variable names and type 2000 assignment statements. 28-Apr-19 AHD c 2010
Fortunately, by using a list, we get around this problem . . . 28-Apr-19 AHD c 2010
What is a list? First consider a simple variable, the type we have used so far: result1 = 67 28-Apr-19 AHD c 2010
After this statement Python reserves an area of memory which is referred to by the name result1 which is big enough to hold the address of the integer object, for example the number 67. Imagine this area of memory as a mail box: result1 . 67 28-Apr-19 AHD c 2010
A List Variable Consider a list variable as a line of these boxes, each box is referred to by a number: 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010
result[0], result[1] and so on... 0 1 2 3 4 5 6 7 result The line of boxes is known as a list. A list variable is given a name, just like the simple variable, (e.g. result), and each box is referred to by its number: result[0], result[1] and so on... 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010
Important The number of the box is known as the subscript or index of the list element (box). 28-Apr-19 AHD c 2010
Note: In Python, the index of a list always starts at 0. Important Note: In Python, the index of a list always starts at 0. 28-Apr-19 AHD c 2010
Elements of a list may be of any data type The individual elements of a Python list do not need to be all of the same data type. A Python list can contain a mixture of strings, integers and float objects, indeed a mixture of any type of object. 28-Apr-19 AHD c 2010
Assigning values to lists Note: for simplicity, the diagram here shows the integer values in each element, but in reality, the elements of a list store references to the data values. result = [0,0,0,0,0,0,0,0] result[0] = 75 result[1] = 90 result[4] = 72 0 1 2 3 4 5 6 7 75 90 72 28-Apr-19 AHD c 2010
How big are the list elements? An element is one of the boxes making up the list. Each element holds the address of the object to which it refers. Hence, each element has size in bytes sufficient to hold an address. 28-Apr-19 AHD c 2010
A List Variable Consider an list variable as a line of adjacent memory locations. The elements are all the same size. 0 1 2 3 4 5 6 7 result 28-Apr-19 AHD c 2010
Python Lists 7.1 Introduction to Lists 7.2 List Operations 28-Apr-19 AHD c 2010
List operations The best way to understand lists is to see them in action... The first example program creates a list of integers, and accesses each item in the list (each element) by its index. result = [0,0,0,0,0,0,0,0] print result result[0] = 75 result[1] = 90 result[4] = 72 print result[0] print result[1] 07-01.py 28-Apr-19 AHD c 2010
An empty list This example program creates an empty list and shows the result of a print on an empty list. list1 = [] print list1 07-02.py 28-Apr-19 AHD c 2010
Appending to an empty list This example program creates an empty list and shows the result of a print on an empty list, appends an item, and prints again. list1 = [] print list1 list1.append(67) print list1[0] list1.append("spam") print list1[1] 07-03.py 28-Apr-19 AHD c 2010
A list of lists This example program creates a list of lists and prints out the list and its elements. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list3=[list1,list2] print list3 print list3[0] print list3[1] 07-04.py 28-Apr-19 AHD c 2010
Accessing the last element in a list This example program creates a list and prints out the last element by using index number -1. list1 = [1,2,3,6,7,8,9,10] print list1 print list1[0] print list1[1] print list1[-1] print list1[-2] 07-05.py 28-Apr-19 AHD c 2010
Deleting items from a list This example program creates a list and deletes selected elements of the list. list1 = [1,2,3,4,5,6,7,8,9,10] print list1 del list1[0] del list1[-1] 07-06.py 28-Apr-19 AHD c 2010
Repeating (multiplying) lists using * print list1 print list1 * 3 list1 = list1 * 2 07-07.py The output: [1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3] 28-Apr-19 AHD c 2010
Lists can be joined together using the + symbol Joining lists together using the + symbol is a process known as concatenation. list1 = [1,2,3] print list1 list2 = [4,5,6] print list2 list1 = list1 + list2 list1 = list1 + list1 [1, 2, 3] [4, 5, 6] [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] 07-08.py 28-Apr-19 AHD c 2010
Indexing lists using the [] operator Any element of a list can be accessed by indexing. list1 = ["Anne", "Dawson", 666] print list1[0], list1[2] prints Anne 666 07-09.py 28-Apr-19 AHD c 2010
Slicing lists using the [] operator Any sublist of a list can be obtained by using the [] operator. list1 = [2,4,6,8,10,12,14,16,18,20] print list1[0:1],list1[5:7] prints [2] [12, 14] 07-10.py 28-Apr-19 AHD c 2010
Finding the length of a list using len The length of any list can be determined using the len method. list1 = ["Anne","was",'here','testing',1,2,3] list2 = [1,2,3,4] list3 = [] print len(list1), print len(list2), print len(list3) prints 7 4 0 07-11.py 28-Apr-19 AHD c 2010
List iteration The following example shows that you can iterate over lists in loops using for statements. list = [1,2,3,"Spam",4,5] for i in list: print i, 07-12.py The output: >>> 1 2 3 Spam 4 5 28-Apr-19 AHD c 2010
List membership The following example shows that you can test lists for membership with the in expression operator: list = [1,2,3,"Spam",4,5] print "Spam" in list 07-13.py The output: >>> True 28-Apr-19 AHD c 2010
List Methods Python has a number of built-in list methods which enable lists to be sorted, reversed, appended, etc. For a full list of available methods, refer to Python's web page: http://docs.python.org/lib/typesseq-mutable.html 07-14.py 28-Apr-19 AHD c 2010
List Textbook References http://www.swaroopch.com/notes/Python_en:Data_Structures#List 28-Apr-19 AHD c 2010
This presentation uses the following program files: http://www.annedawson.net/PythonPrograms.txt 07-01.py 07-02.py 07-03.py 07-04.py 07-05.py 07-06.py 07-07.py 07-08.py 07-09.py 07-10.py 07-11.py 07-12.py 07-13.py 07-14.py 28-Apr-19 AHD c 2010
End of Python_Lists.ppt 28-Apr-19 AHD c 2010
Last updated: Friday 29th May 2009, 14:38 PT, AHD 28-Apr-19 AHD c 2010