Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Iterate Over a List of Lists in Python
Next article icon

Iterating over multiple lists simultaneously allows you to process elements from different lists at the same time. This can be especially useful when dealing with related data stored in multiple lists.

Python provides several methods to achieve this, making it easy to work with multiple iterables in a synchronized manner.

Ways to Iterate Over Multiple Lists Simultaneously

You can use the following methods to iterate over multiple lists in Python:

Let's explore each method in detail with examples.

1. Using zip()

zip()function allows you to iterate over multiple lists at once. It pairs elements from each iterable together, stopping when the shortest list is exhausted.

Python
importitertoolsnum=[1,2,3]color=['red','white','black']value=[255,256]fora,b,cinzip(num,color,value):print(a,b,c)

Output
1 red 2552 white 256

Explanation:

  • zip():This function pairs the elements of the lists. It iterates until the shortest list ends.
  • The output shows each tuple of paired elements from the three lists

2. Using itertools.zip_longest()

Unlike zip(),itertools.zip_longest() continues iteration until all lists are exhausted. If one list is shorter, it fills the missing values with None (or a specified fillvalue).

Python
importitertoolsnum=[1,2,3]col=['red','white','black']val=[255,256]fora,b,cinitertools.zip_longest(num,col,val):print(a,b,c)

Output
1 red 2552 white 2563 black None

Explanation: itertools.zip_longest() ensures that iteration continues even when one list is exhausted, filling missing values withNoneby default.

3. Using itertools.zip_longest() with fillvalue

You can specify a default value instead of None when usingitertools.zip_longest().

Python
importitertoolsnum=[1,2,3]col=['red','white','black']val=[255,256]fora,b,cinitertools.zip_longest(num,col,val,fillvalue=-1):print(a,b,c)

Output
1 red 2552 white 2563 black -1

Explanation: when one list is shorter, the missing values are replaced with the specified fillvalue (-1in this case).

Note :Python 2.x had two extra functions izip() and izip_longest(). In Python 2.x, zip() and zip_longest() used to return list, and izip() and izip_longest() used to return iterator. In Python 3.x, there izip() and izip_longest() are not there as zip() and zip_longest() return iterator.

4. Using enumerate() to Iterate with Index

Theenumerate() function helps when you want to iterate over a list and keep track of the index of each element. You can use this index to access corresponding elements from other lists.

Python
l1=[1,2,3]l2=['a','b','c']l3=['x','y','z']fori,eleinenumerate(l1):print(ele,l2[i],l3[i])

Output
1 a x2 b y3 c z

Explanation:enumerate() function adds a counter to each element of the iterable. The index returned by enumerate() is then used to access corresponding elements in the other lists.

5. Using Generator Expressions with zip()

You can also use generator expressions with zip() to iterate over multiple lists simultaneously in a more compact form.

Python
foritem1,item2,item3inzip((1,2,3),('a','b','c'),(True,False,True)):print(item1,item2,item3)

Output
1 a True2 b False3 c True

Explanation:Generator expressions allow you to iterate over data without storing all elements in memory at once. This is useful for large datasets or when performance is critical.

Related Articles:


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp