Categories
Python Answers

How to do double Iteration in list comprehension with Python?

Spread the love

Sometimes, we want to do double Iteration in list comprehension with Python.

In this article, we’ll look at how to do double Iteration in list comprehension with Python.

How to do double Iteration in list comprehension with Python?

To do double Iteration in list comprehension with Python, we can use 2 for expressions.

For instance, we write

l = [word for sentence in text for word in sentence]

to loop through the sentence in the text list with

for sentence in text

In it, we have another loop to loop the the word in the sentence list.

for word in sentence

And then we put the word strings in the list l.

Conclusion

To do double Iteration in list comprehension with Python, we can use 2 for expressions.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *