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.