Double Iteration in List Comprehension

Double Iteration In List Comprehension

List comprehensions are one of the many things that Python has which differentiate Python from other languages and prove it better. It’s essential to learn as it leads to concise and better-formatted code. It can be difficult to understand and has very error-prone syntax.

So, in this article, we will dive deep into list comprehension in Python and understand Double Iteration in list comprehension.

What is a list?

A list is a data structure that stores a collection of data. It is derived from the most basic data structure array. An array is a continuous and contagious collection of data of similar data types. In general, we refer list as similar to an array, but they both are different. An array has all the elements of the same data type, whereas a list can have elements of different data types.

num_list = [1,2,3,4,5]
print(num_list)

OUTPUT
[1,2,3,4,5]

In the above code, we created a list of numbers from 1 to 5.

What is List Comprehension?

List comprehension is adding elements to a list with a shorter syntax. It consists of a list containing a for loop. The loop generates elements inside the list. List comprehension has a bit of confusing syntax, making it hard to understand. So let’s try to use it in a simple example to understand it properly.

The syntax is <list_name> = [<expression> for <iterator> in <data_structure>]. The expression will be appended to the list in each iteration. The iterator is the variable that we use in the expression. The data structure can be a list, range, or even a set we will traverse.

nums_list = [i for i in range(1,6)]
print(nums_list)

OUTPUT
[1,2,3,4,5]

In the above code, we used list comprehension to create a list of numbers containing numbers from 1 to 5. The way we did it is we wrote the name of the variable, then we equated it to a list that contains a for loop. This for loop helps us in adding elements to the list. The iterator i gets appended to the list in each iteration.

Related: Learn List Comprehension in depth.

Now, let’s compare the list comprehension with the classic for loop and check which is faster.

Comparing Runtime for classic for loop vs. list comprehension

from time import time
start = time()
nums_list = [i for i in range(10**8)]
end = time()
print(f"Time taken using list comprehension:{end-start}")
start = time()
nums_list = []
for i in range(10**8):
    nums_list.append(i)
end = time()
print(f"Time taken using classic for loop:{end-start}")
Comparsion Between List Comprehension And Classic For Loop
Comparison Between List Comprehension And Classic For Loop

In the above code block, we imported the time function from the time module. Then we stored the time at the moment in the start variable and then created a list of a hundred million elements using list comprehension. After it’s done, we stored the ending time in the variable end. Then when we subtract the start time from the end time, we get the time taken for appending 108 elements in the list using list comprehension. Then we did the same thing again, but this time we used for loop.

Using classic for loop took almost 16 seconds, and using list comprehension took 9.48 seconds. This obvious difference makes list comprehension a better alternative for appending elements in a list.

Advantages of List Comprehension

List comprehension has many advantages. Some of them are listed below.

  • Has a short syntax.
  • It is faster than the classic for loop.
  • It can be easily integrated with lambda functions and if statements.
  • It can create complex patterns like the list of reversed strings or the list of the nth power of numbers of a particular range.

Using if statement in List Comprehension

What is an if-statement?

‘If’ statements are a way to write conditional statements in code. In Python, we write conditional statements using the keyword if. The syntax for the if statement is very simple in Python.

if <condition>:
        #Code to run if the condition satisfies

Related: Learn more about if-statements in Python.

How to use an if-statement in a list comprehension?

‘If’ statements really enhance the abilities of list comprehension. You can create a list of complex patterns using a nested if statement in the for loop of list comprehension. Let’s see how we can use an if statement in a list comprehension.

Creating a list of even numbers

even_nums = [i for i in range(10) if i%2==0]
print(even_nums)

OUTPUT
[0,2,4,6,8]

In the above block of code, created a list of even numbers using an if statement inside the list comprehension, which results in True only when the iterator gives remainder 0 when divided by 2. If the result of the if statement is True, then to number will be added to the list else it won’t. This way, we got a list of even numbers.

Using the Lambda function with List Comprehension

What is a lambda function?

Lambda functions, also known as shorthand functions, create functions in Python with a short syntax. They are really convenient, simple, and yet extremely powerful.

The syntax is lambda <variable>:<expression>.

Related: Learn more about lambda functions in Python.

How to use a lambda function with list comprehension?

square = lambda x:x**2
squared_nums = [square(i) for i in range(1,11)]
print(squared_nums)

OUTPUT
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

In the above block of code, first, we created a lambda function to square a given number and then generated a list of squares of numbers from 1 to 10 using this square function and list comprehension.

How to do a double iteration in list comprehension?

Okay, so before moving to our leading problem, let’s summarize what we did now. First, we learned what list comprehension is. Then we learned to integrate if statements in a list comprehension. Then we learned to use the lambda function with list comprehension. Let’s now see how we can do double iteration in a list comprehension.

List comprehension is complicated by itself. Doing double iteration in a list comprehension can be challenging to understand. So let’s try to understand with the help of some common places where list comprehensions can be used.

Creating a list of all possible combinations of 2 numbers up to a number n

n = 5
combinations = [(x,y) for x in range(1,n+1) for y in range(1,n+1)]
print("All possible combination of pairs from 1 to 5 are:",combinations)
Code And Output For Double Iteration In List Comprehension
Code And Output For Double Iteration In List Comprehension

The above code generated a list of all possible pairs of two numbers with numbers from 1 to n. We used 2 lists for loops in single-list comprehension for this purpose. We created (x, y) pairs with x being all the numbers from 1 to 5 and y being all the numbers from 1 to 5, too, using for loops.

Creating a matrix of 0s of size (m, n)

m = 3
n = 4
matrix = [[0 for i in range(m)] for j in range(n)]
# printing the matrix
for row in matrix:
    print(row)
Creating A Matrix Using List Comprehension
Creating A Matrix Using List Comprehension

We created a matrix of 0s with n rows and m columns in the above code block using list comprehension. We nested one list comprehension into another. First, we used list comprehension to create a list of m numbers then we put this list n times into another list. This way we got a matrix of (m,n) size full of 0s.

Flattening a matrix

Flattening the matrix is a process in which we convert a multi-dimensional list into a single-dimensional list. Let’s try to understand by coding it.

matrix = [
    [1,2,3],
    [4,5,6],
    [7,8,9]
]
flat_matrix = [num for row in matrix for num in row]
print(flat_matrix)

OUTPUT
[1,2,3,4,5,6,7,8,9]

In the above block of code, took a list of list of numbers from 1 to 3, 4 to 6, and 7 to 9 and converted it into a single-dimensional list of numbers from 1 to 9.

Dictionary comprehension

What is a dictionary?

A dictionary is a data structure that is similar to hashmaps. It stores data in key: value pairs. Searching in a dictionary takes O(1) time. It can be used when there’s a time constraint.

Related: Learn dictionaries in Python in depth.

What is dictionary comprehension?

Like list comprehension, dictionary comprehension creates a dictionary with a short syntax.

The syntax is <dictionary_name> = [<key>:<value> for <iterator> in <data_structure>]. Here the key is the key, and value is the value of the key, which will be added to the dictionary in each iteration. The iterator is the variable that we use in the expression. The data structure can be a list, range, or even a set in which we will be traversing

Let’s check out how to use dictionary comprehension.

Related: Learn Dictionary Comprehension in depth.

How to use dictionary comprehension?

squares = {x:x**2 for x in range(10)}
print(squares)

OUTPUT
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

In the above code block, we created a dictionary with numbers from 0 to 9 as keys and their squares as their values using the syntax of dictionary comprehension we learned above.

Conclusion

List comprehension is an essential technique to master to improve Python programming. They are amazing and easy to use. It has a bit of hard syntax, but once you understand it, it becomes really easy to use. It has a lot of applications and can be used in many ways depending on your creativity. It is also faster than a classic for loop, saving up runtime on bigger work.

References

Official Python Documentation for list comprehension.

Stack Overflow answer for the same question.