Double Iteration in List Comprehension - AskPython (2024)

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 timestart = 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}")
Double Iteration in List Comprehension - AskPython (1)

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**2squared_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 = 5combinations = [(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)
Double Iteration in List Comprehension - AskPython (2)

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 = 3n = 4matrix = [[0 for i in range(m)] for j in range(n)]# printing the matrixfor row in matrix: print(row)
Double Iteration in List Comprehension - AskPython (3)

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.

Double Iteration in List Comprehension - AskPython (2024)

FAQs

Is list comprehension faster than iteration? ›

In general, map and list comprehensions are faster than loops for iterating over a collection in Python. However, the relative performance of these methods depends on the specific use case.

Can you have multiple expressions in list comprehension? ›

The rule of thumb is to avoid using more than two expressions in a list comprehension. This could be two conditions, two loops, or one condition and one loop.

What is the alternative to list comprehension in Python? ›

List comprehension is a more laconic alternative to for-loops (including the nested ones), lambda function, Python built-in functions map(), filter() and reduce(), and conditionals.

How do you iterate through a list multiple times in Python? ›

There are several methods to iterate over a list in Python:
  1. For Loop: The most common way to iterate over a list.
  2. While Loop: Uses an index to iterate over the list.
  3. List Comprehension: A concise way to create a new list by iterating over an existing one.
  4. Enumerate: Provides both index and value during iteration.
Jul 7, 2024

How do you iterate a list multiple items in Python? ›

You can use the itertools. chain() function to quickly go through multiple lists sequentially. Here is an example of iterating through lists L1, L2, and L3 using the chain() function. Using itertools is one of the fastest and most memory-efficient ways to go through multiple lists since it uses iterators.

Why list iteration is slower? ›

One reason could be that the list iteration function does not read the head of the list from memory. A fairer benchmark would be to, for example, use functions that calculate the sum of all elements.

Is it better to use list comprehension? ›

List comprehensions are useful and can help you write elegant code that's easy to read and debug, but they're not the right choice for all circ*mstances. They might make your code run more slowly or use more memory.

Which is faster lambda or list comprehension? ›

List comprehensions are fastest, followed by filter() with a predefined filter function, and filter() with an ad-hoc lambda function comes in last.

When not to use list comprehension? ›

Now let's take three examples to understand why you shouldn't be using list comprehensions for tasks that require super complex expressions. Because in such cases, list comprehensions—instead of making your code elegant—make your code difficult to read and maintain.

Does list comprehension reduce time complexity? ›

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration. This is slow.

Can we write every loop as list comprehension? ›

Every comprehension of the list can be rewritten in for loop, but in the context of list interpretation, every for loop can not be rewritten.

Is Python list comprehension lazy? ›

3 Answers. There's no lazy evaluation of lists in Python.

What is more efficient than a list in Python? ›

Tuples are faster than lists in terms of performance. This is because tuples are immutable, and Python does not need to allocate additional memory to accommodate any changes made to them. Lists, on the other hand, are mutable, and Python needs to allocate additional memory every time you modify them.

What are the 4 types of comprehension in Python? ›

There are four types of comprehension in Python for different data types – list comprehension, dictionary comprehension, set comprehension, and generator comprehension.

How do you double each item in a list Python? ›

Call the multiply() function from numpy library and pass test_list and 2 as arguments. The multiply() function will perform element-wise multiplication of the input list by 2 and return a new list.

How to iterate two lists together in Python? ›

  1. You can iterate over two lists in Python using the zip function:
  2. list1 = [1, 2, 3] list2 = ['a', 'b', 'c']
  3. for item1, item2 in zip(list1, list2): print(item1, item2)
Dec 31, 2022

How do you use a double list in Python? ›

Creating 2D List using Naive Method. Here we are multiplying the number of columns and hence we are getting the 1-D list of size equal to the number of columns and then multiplying it with the number of rows which results in the creation of a 2-D list.

How do you iterate two times in Python? ›

Use a third loop: for x in List1: for _ in [0,1]: for y in x: ... This iterates over each element of List1 twice, as in the original code, without explicit index wrangling. Save this answer.

Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6695

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.