Last updated: Monday 1st March 2021, 11:46 PT, AD
Introduction to Lists in Python 3.x (and Python 2.x) See videos at the bottom of this page. Most real-world Python programs contain lists. Lists allow you to collect objects (ints, floats, strings), so that you can treat them as a group. For example, you might want to keep a list of midterm exam scores for a computer class so that you can process it later, to find the average score for the class. Lists have left-to-right positional ordering, with index capability. This means that you can specify (with a number) which item in the list to work on, the first, second etc, moving from left to right. Lists can grow and shrink in place (i.e. mutable with no need to create another object (as with strings). This means you can add items to a list, to the front or end, or in fact any position referred to by the index number. Lists are heterogeneous; i.e. a single list can contain a variety of object types, including lists. For example, you could have a list containing strings and integers, perhaps the names and scores of students in a computer class. 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 (address of - i.e. location in memory) to 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.) 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. 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 . . . 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? 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. Fortunately, by using a list, we get around this problem . . . What is a list? First consider a simple variable, the type we have used so far:result1 = 67
Important The number of the box is known as the subscript or index of the list element (box). Note: In Python, the index of a list always starts at 0. 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.
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.
There is an error in program 07-12.py in the following screen.
The name "list" should be replaced with "list2".
There is an error in program 07-13.py in the following screen.
The name "list" should be replaced with "list2".
Two-Dimensional Python lists - see Program 07-15.py Python 3 Lists Powerpoint and: Python 3 Programs http://www.annedawson.net/python3programs.html 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 07-15.py 07-16.py Python 2.x Lists Powerpoint and: Python 2.x Programs http://www.annedawson.net/pythonprograms.html 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 07-15.py Python 3 Lists Powerpoint and: Python 3 Programs Programs 07-01.py - 07-16.py Python 2.x Lists Powerpoint and: Python 2.x Programs Programs 07-01.py - 07-15.py