Home Lesson-5.2

Lesson-5.1

Lists

Introduction

A list in Python is a data structure that is used to store a sequence of objects. Some examples are given below:

 

Iterating through lists

As a list is a sequence, we can iterate through it using for. This is one of the primary uses of the for loop:

The loop variable — num — picks one item at a time from the sequence. In the body of the loop, we are just printing this item. We can rewrite the code given above using a while loop:

Finally, we can also use the for loop to iterate through the indices of the list. For this, we take the help of the range function.

In the example given above, len(numbers) is equal to 4. So, the range sequence will be 0, 1, 2, 3. index is the loop variable that iterates through this sequence.

Methods 2 and 3 are very similar. Both iterate through the sequence of indices, and use list indexing to access the corresponding element in the list. The only difference is that method-2 uses while, while method-3 uses for. Method-1 stands out from the other two as it directly pulls elements from the sequence.

 

Growing a list

Lists are typically used in problems where we wish to store a collection of items. Usually, we start with an empty list. Python provides two ways to create an empty list:

Both list1 and list2 are empty lists. The interpreter doesn't mind spaces between the opening and closing braces, so list1 = [ ] also works. Given an empty list, how do we add items to it? Python provides two ways to do this:

Both lists end up having just the one element. The first method is called list concatenation, i.e., two lists are being concatenated or combined together. Treat concatenation like joining two compartments of a train together. It is very similar to string concatenation. The second way uses a method called append that is essentially a function defined for the list type. Append adds elements at the end of the list.

Consider the following problem:

Generate the list of positive integers less than 100 that are divisible by 3.

There are at least two ways of doing this. The first one uses while:

The next method uses for:

 

Operations on Lists

We have already seen how the + operator works with lists:

This gives the concatenated output:

The order matters when two lists are being concatenated! The next is the * operator:

This replicates the list. The following is the output:

Two lists are equal if they have the same sequence of elements:

This results in:

Finally, two lists can be compared with the > or the < operator. List comparison works very similar to string comparison, in that it uses lexicographic ordering. We looked at this in the first chapter:

Lexicographic ordering

First element from both lists are compared. If they differ this determines the outcome of the comparison. If they are equal, then the second element of both the lists are compared. This process continues until either list is exhausted.

Some example comparisons:

All four of them result in True.

 

Useful Functions

Let us look at some built-in functions that operate on lists:

What happens if a is a list of strings? What would max(a) and min(a) produce?

We have come across the range object and seen how useful it was in iterating through a sequence. So far range has been associated with the for loop. Its time has come to break out of the loopy prison:

This gives range(0, 10) as an output. This is a sequence that we can iterate over. Python provides a way of turning this object into a list:

This gives [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as the output.