Guide to Python Lists and List Comprehension

A Python list is an iterable and indexable means to store data under a single variable.
Andrew Wood  •   02 June 2022
Andrew Wood  •   Last Updated: 02 June 2022

Python lists are a fundamental part of the language and one of the keys with which developers can write efficient, easy to understand, and readable code. Lists are a powerful tool, and list comprehensions especially can be used to provide a lot of functionality in a single well-written line of code.

Before we get started with list comprehension and its syntax which often confuses newer developers, let’s first go over the basics of lists. We’ll show how by combining the list functionality with a few additional methods like map(), filter(), and zip(), we can write clear, efficient and pythonic code.

List Basics

A list is simply an iterable and indexable means to store data under a single variable. Lists are created with square brackets and can house any data type you wish (you can store different types of data at the different indexes if you want). You can also nest data within a list, for example storing a list within a list at a specific index.

To access the data within a list you use an index. The first item in the list is indexed at [0], the second at [1] and so on.

Lists are an example of ordered data, the items in the list are all stored at a defined index and will not change unless you make the change yourself.

>>> animal_list = ["dog","cat","rabbit","horse","mouse","fish"]

# extract the first animal
>>> print(animal_list[0])
"dog"

# add a tiger to the list
>>> animal_list.append("tiger")
>>> print(animal_list)
["dog","cat","rabbit","horse","mouse","fish","tiger"]

# print every item in the list
>>> for animal in animal_list:
>>>    print(animal)
"dog"
"cat"
"rabbit"
"horse"
"mouse"
"fish"
"tiger"

Using the map and filter functions on a List

The map() function executes a specific function for each item in an iterable. Since a list is iterable (can iterate through the list with a for() loop), the map() function is often used in conjunction with a list to change modify the value of the data at each index.

map and Lambda Functions

A lambda function is a simple anonymous function (not named like you do with a Python def) that can be written and executed inline without all the syntax of a python def.

Together the map() and lambda functionality can be used to perform an operation on every item in a list as demonstrated below.

>>> number_list = [3,6,8,22,34,100]

# multiply each number by 3
>>> new_number_list = list(map(lambda num: 3*num, number_list))
>>> print(new_number_list)
[9, 18, 24, 66, 102, 300]

The Use of map with a Python Function

The map() function also works with a standard Python def function, which may be more suitable than a lambda function when the functionality required is a little more detailed.;

In the example below we use map() and a def function to multiply each number in a list by three and then divide that number by two if the result is even.

number_list = [3,6,8,22,34,100]

# Map function will work with a def
# multiply each number in the list by 3 and divide by 2 if result is even 

def myfunc(num):
    newnum = num*3
    if newnum % 2 == 0: # even
        newnum = int(newnum/2)
    return newnum

def_number_list = list(map(myfunc,number_list))
>>> print(def_number_list)
[9, 9, 12, 33, 51, 150]

Using the filter Function on a List

The filter() method takes a function and a list, and returns a subset list of the elements where the function evaluates to True.

We show this in the example below where we filter a list to only return those numbers divisible by three.

>>> pre_filter_list = [3,4,8,9,12,15,16]

# Filter a list of numbers to only include those divisible by 3.
>>> filtered_list = list(filter(lambda n: n%3==0,pre_filter_list))

>>>print(filtered_list)
[3, 9, 12, 15]

 

Python Dictionaries and the Use of zip

The zip() function takes two iterables (for example a list) and stiches them together into a zip object which is an iterator of tuples.

This means we can take two lists of equal length, and zip them together such that each index of the new zip object contains a tuple of the corresponding two list entries.

animal_list = ["dog","cat","rabbit","horse","mouse","fish"]
animal_color = ["brown","orange","blue","green","pink","red"]

animals = list(zip(animal_list,animal_color))
>>>print(animals)
[('dog','brown'),('cat','orange'),('rabbit','blue'),
 ('horse','green'),('mouse','pink'),('fish','red')]

>>> print(animals[0][1])
"brown"

The zip() function returns a set of tuples comprised of the corresponding index contents of two lists. A dictionary is composed of key and value pairs where the key is used to access the information stored in the dictionary. The zip() function provides a convenient means to stitch our keys to our values before passing the information into a dictionary.

In our example below we make use of the zip() function twice. First we use zip() to stitch together information about the animal: its colour and age. This tuple forms the value componnent of the key,value pair in the dictionary. The key will be the type of animal. This must be zipped into the newly created tuple, before being added to the dictionary. 

>>> animal_list = ["dog","cat","rabbit","horse","mouse","fish"]
>>> animal_color = ["brown","orange","blue","green","pink","red"]
>>> animal_age = [3,5,2,15,1,3]

>>> animal_info = zip(animal_color,animal_age)

>>> print(list(animal_info))
[("brown",3),("orange",5),("blue",2),("green",15),("pink",1),("red",3)]

>>> animals = zip(animal_list,animal_info)
>>> print(list(animals))
[("dog",("brown",3)),("cat",("orange",5)),("rabbit",("blue",2)),
 ("horse",("green",15)),("mouse",("pink",1)),("fish",("red",3))]
animal_dict = {}
for name,info in animals:
    animal_dict[name]= info
    
>>> print(animal_dict)
{'dog': ('brown', 3), 
    'cat': ('orange', 5), 
    'rabbit': ('blue', 2), 
    'horse': ('green', 15), 
    'mouse': ('pink', 1), 
    'fish': ('red', 3)
}

List Comprehension

The list comprehension syntax offers a more concise way to create a new list based on the values of an existing list.

Let’s say that you wish to multiply every value in a list by 5. One way to do this is with a for loop.

oldlist = [1,3,5,7,9,18]

newlist = []
for value in oldlist:
    newlist.append(5*value)
>>> print(newlist)
[5, 15, 25, 35, 45, 90]

List comprehension allows you to achieve the same result in a single line. The syntax is sometimes confusing to newer Python developers, so we’ll go through it with a few examples of increasing complexity.

newlist = [expression for item in iterable if condition(s) == True]

Let's use list comprehension to repeat the example above where we multiply each item in a list by 5. 

>>> newlist = [5*i for i in oldlist]
>>> print(newlist)
[5, 15, 25, 35, 45, 90]

List Comprehension with Conditionals

We can include conditionals (if statements) in our list comprehension statement if we need.

You can think of the conditional(s) in the same way as filter() which we discussed earlier in this post. Let’s take another list of numbers and multiply all the numbers in the list by 3, if and only if the original number is divisible by 3.

oldlist = [1,3,5,7,9,18]

newlist = [3*i for i in oldlist if i%3==0]
# only the numbers in oldlist divisible by 3 are added to newlist 
>>> print(newlist)
[9, 27, 54]

We can add multiple conditional statements to the list comprehension which provides us a way to apply multiple filters to our list.

In this last example we multiply the numbers in the list by 3 if (i) the original number is divisible by 3 AND (ii) the original number is greater than or equal to 9.

oldlist = [1,3,5,7,9,18]

newlist = [3*i for i in oldlist if (i%3==0 and i>=9)]
>>> print(newlist)
[27, 54]

Summary

List and list comprehension is Python is a powerful method of writing efficient code that is easy to understand, read, and follow. The main concepts covered in this tutorial on lists and list comprehension are:

  • Lists are an ordered, iterable, and indexable means to store data.
  • The map() function executes a specific function for each item in an iterable (such as a list).
  • Lambda functions allow for simple, inline, anonymous functions to be written, and work well when combined with a map() function to create a new list from the values in the original list.
  • The filter() function takes a function and a list, and returns a subset list of the elements where the function evaluates to True.
  • The zip() function is used to stich together two iterables (lists) into a single tuple containing the corresponding indexed list data. This is often used to create a dictionary from two lists where one list represents the keys and the other list the value pairing.
  • List comprehension offers a more concise way to create a new list based on the values of an existing list. Conditionals may be added to the list comprehension which act in the same way as the filter() method.
Share this
Comments
Canard Analytics Founder. Python development, data nerd, aerospace engineering and general aviation.
Profile picture of andreww
Share Article

Looking for a partner on your next project?

Contact Us