More on Lists
Last updated
Last updated
in
operatorThe in
operator seen for strings also works on lists.
The most common way to traverse the elements of a list is with a for
loop. The syntax is the same as for strings:
This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine the functions range
and len
:
This loop traverses the list and updates each element. len
returns the number of elements in the list. range
returns a list of indices from 0
to n-1
, where n
is the length of the list. Each time through the loop i
gets the index of the next element. The assignment statement in the body uses i
to read the old value of the element and to assign the new value.
A for
loop over an empty list never executes the body:
Although a list can contain another list, the nested list still counts as a single element. The length of the list ['spam', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
is four.
The +
operator concatenates lists:
Similarly, the *
operator repeats a list a given number of times:
The first example repeats [0]
four times. The second example repeats the list [1, 2, 3]
three times.
Python provides methods that operate on lists. For example, append
adds a new element to the end of a list:
extend
takes a list as an argument and appends all of the elements:
This example leaves t2
unmodified.
sort
arranges the elements of the list from low to high:
List methods are almost all void; they modify the list and return None
. If you accidentally write
t = t.sort()
, you will be disappointed with the result.
To add up all the numbers in a list, you can use a loop like this:
total
is initialized to 0
. Each time through the loop, x
gets one element from the list. The +=
operator provides a short way to update a variable. This augmented assignment statement:
is equivalent to:
As the loop executes, total
accumulates the sum of the elements; a variable used this way is sometimes called an accumulator. Adding up the elements of a list is such a common operation that Python provides it as a built-in function, sum
:
An operation like this that combines a sequence of elements into a single value is sometimes called reduce. Sometimes you want to traverse one list while building another. For example, the following function takes a list of strings and returns a new list that contains capitalised strings:
result
is initialised with an empty list; each time through the loop, we append the next element. So result
is another kind of accumulator. An operation like capitalise_all
is sometimes called a map because it "maps" a function (in this case the method capitalize
) onto each of the elements in a sequence.
Another common operation is to select some of the elements from a list and return a sublist. For example, the following function takes a list of strings and returns a list that contains only the uppercase strings:
isupper
is a string method that returns True
if the string contains only upper case letters. An operation like "only_upper" is called a filter because it selects some of the elements and filters out the others.
Most common list operations can be expressed as a combination of map, filter and reduce. Because these operations are so common, Python provides language features to support them, including the built-in function map
and an operator called a comprehension.
Exercise: Write a function cumulative_sum
that takes a list of numbers and returns the cumulative sum; that is, a new list where the element is the sum of the first elements from the original list. For example, the cumulative sum of [1, 2, 3]
is [1, 3, 6]
.
There are several ways to delete elements from a list. If you know the index of the element you want, you can use pop
:
pop
modifies the list and returns the element that was removed. If you don't provide an index, it deletes and returns the last element. If you don't need the removed value, you can use the del
operator:
If you know the element you want to remove (but not the index), you can use remove
:
The return value from remove
is None
. To remove more than one element, you can use del with a slice index:
As usual, the slice selects all the elements up to, but not including, the second index.
The sorted()
function in Python is a built-in function that sorts the elements of a given iterable in a specific order (ascending or descending) and returns it as a list. The sorted()
function takes two optional parameters:
reverse
: A boolean value that specifies whether the list should be sorted in descending order (True
) or ascending order (False
). The default value is False
.
key
: A function that takes a single element from the iterable as input and returns a value that will be used to sort the element. This allows you to sort the iterable based on a custom criteria.
The sorted()
function can be used to sort any iterable object, such as lists, tuples, strings, and dictionaries. For example, the following code sorts a list of numbers in ascending order:
We can also sort a list in descending order using the key parameter reverse
:
The sorted()
function can also be used to sort a list of strings in alphabetical order:
The sorted()
function is a powerful tool that can be used to sort any iterable object in a variety of ways. It is a versatile function that can be used in a variety of programming tasks. We will revisit the function later in the book when we have learned the concept of lambda functions.
A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert from a string to a list of characters, you can use list
:
Because list
is the name of a built-in function, you should avoid using it as a variable name. I also avoid l
because it looks too much like 1
on some fonts. So that's why I use letters
instead.
The list
function breaks a string into individual letters. If you want to break a string into words, you can use the split
method:
An optional argument called a delimiter specifies which characters to use as word boundaries. The following example uses a hyphen as a delimiter:
join
is the inverse of split
. It takes a list of strings and concatenates the elements. join
is a string method, so you have to invoke it on the delimiter and pass the list as a parameter:
In this case the delimiter is a space character, so join
puts a '-' between words. To concatenate strings without delimiters, you can use the empty string, ''
, as a delimiter.
If we execute these assignment statements:
We know that a
and b
both refer to a string, but we don't know whether they refer to the same string. There are two possible states:
In one case, a
and b
refer to two different objects that have the same value. In the second case, they refer to the same object. To check whether two variables refer to the same object, you can use the is
operator.
In this example, Python only created one string object, and both a
and b
refer to it. But when you create two lists, you get two objects:
So the state diagram looks like this:
In this case we would say that the two lists are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical.
Until now, we have been using "object and value" interchangeably, but it is more precise to say that an object has a value. If you execute [1,2,3]
, you get a list object whose value is a sequence of integers. If another list has the same elements, we say it has the same value, but it is not the same object.
If a
refers to an object and you assign b = a
, then both variables refer to the same object:
The state diagram looks like this:
The association of a variable with an object is called a reference
. In this example, there are two references to the same object. An object with more than one reference has more than one name, so we say that the object is aliased.
If the aliased object is mutable, changes made with one alias affect the other:
Although this behaviour can be useful, it is error-prone. In general, it is safer to avoid aliasing when you are working with mutable objects.
For immutable objects like strings, aliasing is not as much of a problem. In this example:
It almost never makes a difference whether a
and b
refer to the same string or not.
Certainly! Let's explore list comprehension in Python.
List comprehension is a concise and powerful way to create lists in Python. It allows you to generate new lists by applying an expression to each item in an existing iterable (such as a list, tuple, or range) and optionally filtering the items based on a condition. The result is a new list that often requires fewer lines of code compared to traditional for loops.
Here's the basic syntax of a list comprehension:
expression
is the operation to perform on each item.
item
represents the current item in the iterable.
iterable
is the source of data (e.g., a list or range).
condition
(optional) filters the items based on a specified condition.
Creating a List of Squares:
Filtering Even Numbers:
Creating a List of Combinations:
Working with Strings:
List comprehensions are not only concise but also efficient, making them a favourite among Python developers for tasks like data transformation and filtering. They improve code readability and help you write Pythonic, elegant code. However, for more complex operations, it's essential to strike a balance between brevity and clarity to maintain code maintainability.
When you pass a list to a function, the function gets a reference to the list. If the function modifies a list parameter, the caller sees the change. For example, delete_head
removes the first element from a list:
Here's how it is used:
The parameter lst
and the variable letters
are aliases for the same object. The stack diagram looks like this:
Since the list is shared by two frames, I drew it between them.
It is important to distinguish between operations that modify lists and operations that create new lists. For example, the append
method modifies a list, but the +
operator creates a new list:
This difference is important when you write functions that are supposed to modify lists. For example, this function does not delete the head of a list:
The slice operator creates a new list and the assignment makes t
refer to it, but none of that has any effect on the list that was passed as an argument.
An alternative is to write a function that creates and returns a new list. For example, tail
shown below returns all but the first element of a list:
This function leaves the original list unmodified. Here's how it is used:
Exercise: Write a function called chop
that takes a list as parameter and modifies it, removing the first and last elements, and returns None
. Then write a function called middle
that takes a list and returns a new list that contains all but the first and last elements.
a
and b
.a
and b
.a
and b
)
to the same object [1,2,3]
.