Chapter 9 Lists

9.4 Lists and Loops

Item iteration. Similar to tuples, list objects are iterable, they can be iterated through by using a for loop.

The first for loop above prints a list of objects and their types, while the second for loop above prints the range of integers from 1 to 9, as indicated by the list.

Iteration through a sequence of numbers is frequently needed in programming, and Python provides a built-in function, range, which returns simple number sequences as list objects to facilitate number iteration. The range function can take one, two or three arguments. When a single argument n is given, the range function returns the list [0, 1, …, n − 1]. If n ≤ 0, an empty list is returned. When two arguments s and e are given, the range function returns the list [s, s + 1, s + 2,…, e − 1]. If se, an empty list is returned. When three arguments, s, e and step are given, the range function returns the list [s, s + step, s + 2 ,…, e - 1].

The range function offers a new way to iterate through all the items in an iterable object. Different from using a for statement to iterate through an iterable object directly, the range function allows the use of a for statement to iterate through all the indices from 0 to the length of a sequence object.

Index iteration allows flexible orders of iteration. For example, the code below shows two ways to print the items of a list in the reverse order without modifying the list, both by index iteration.

The example below iterates through every second item in a list:

Index iteration also allows iterating through a half of a sequence object. For example, consider the palindrome number problem again. Index iteration allows the problem to be solved without converting the number into a list and making of copy of the list. The solution is based on the observation that if the ith digit on the left is the same as the ith digit on the right for all i, the corresponding number will be the same after the digits are reversed. Based on symmetry, as long as the left half of a list satisfies the condition above, the whole list satisfies the condition. The idea can be implemented as searching for a digit that is different from its counterpart. If such a digit is found, then the number is not a palindrome. Otherwise it is a palindrome.

Strings and Lists. Two of the most useful methods on strings involve lists of strings. The split method breaks a string into a list of words. By default, any number of whitespace characters is considered a word boundary.

An optional argument called a delimiter can be used to specify which characters to use as word boundaries. The following example uses the string ai as the delimiter:

Notice that the delimiter doesn’t appear in the result.

The inverse of the split method is join. You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.

The list that you glue together (wds in this example) is not modified. Also, you can use empty glue or multi-character strings as glue.

List comprehension. Let’s say we want to create a list from a sequence of values based on some selection criteria. An easy way to do this type of processing in Python is to use a list comprehension. List comprehensions are concise ways to create lists. The general syntax is:

[<expression> for <item> in <sequence> if  <condition>]

where the if clause is optional. For example,

The expression describes each element of the list that is being built. The for clause iterates through each item in a sequence. The items are filtered by the if clause if there is one. In the example above, the for statement lets item take on all the values in the list mylist. Each item is then squared before it is added to the list that is being built. The result is a list of squares of the values in mylist.

Check your understanding

© Copyright 2024 GS Ng.

Next Section - 9.5 Lists and Function Arguments