DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • An AI-Driven Architecture for Autonomous Network Operations (NetOps)
  • Extracting Clean Excel Tables From PDFs Using Python + Docling
  • Regex in Action: Practical Examples for Python Programmers
  • Python and Open-Source Libraries for Efficient PDF Management

Trending

  • How Rule Engines Transform Business Agility and Code Simplicity
  • The Prompt Isn't Hiding Inside the Image
  • Comparing Top Gen AI Frameworks for Java in 2026
  • AI in SRE: What's Actually Coming in 2026
  1. DZone
  2. Coding
  3. Languages
  4. How to Iterate Over Multiple Lists Sequentially in Python

How to Iterate Over Multiple Lists Sequentially in Python

Do you need to process items of multiple Python lists in one go? Here are some simple ways to do this using the itertools library and for loop.

By 
Sreeram Sreenivasan user avatar
Sreeram Sreenivasan
·
May. 20, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
10.0K Views

Join the DZone community and get the full member experience.

Join For Free

Python list is a versatile data structure that allows you to easily store a large amount of data in a compact manner. Lists are widely used by Python developers and support many useful functions out-of-the-box. Often you may need to work with multiple lists or a list of lists and iterate over them sequentially, one after another. There are several simple ways to do this. In this article, we will learn how to go through multiple Python lists in a sequential manner.

Let us say you have the following 3 lists.

Python
 
L1=[1,2,3]
L2=[4,5,6]
L3=[7,8,9]


1. Using itertools.chain()

itertools is a very useful Python library that provides many functions to easily work with iterable data structures such as list. 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.

Python
 
>>> for i in itertools.chain(L1,L2,L3):
        print i
 1
 2
 3
 4
 5
 6
 7
 8
 9


Using itertools is one of the fastest and most memory-efficient ways to go through multiple lists since it uses iterators. This is because iterators only return one item at a time, instead of storing a copy of the entire iterable in memory, as is the case of for loop.

2. Using for Loop

Sometimes you may have a list of lists, as shown below.

Python
 
L4 = [L1, L2, L3]
print L4
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


In such cases, you can use a nested for loop to iterate through these lists.

Python
 
>>> for i in L4:
        for j in i:
               print j         
 1
 2
 3
 4
 5
 6
 7
 8
 9


Alternatively, you can also use itertools.chain() to go through a list of lists.

Python
 
>>> for i in itertools.chain(L4):
         for j in i:
               print j         
 1
 2
 3
 4
 5
 6
 7
 8
 9


3. Using Star Operator

The above-mentioned methods work with most Python versions. But if you use Python 3+, then you can also avail the star (*) operator to quickly unpack a list of lists.

Python
 
for i in [*L1, *L2, *L3]:
    print(i)
    
1
2
3
4
5
6
7
8
9


4. Using itertools.izip()

So far, in each of the above cases, all items of the first list are displayed, followed by all items of the second list, and so on. But sometimes you may need to sequentially process the first item of each list, followed by the second item of each list, and so on. For this kind of sequential order, you need to use the itertools.izip() function. Here is an example to illustrate it.

Python
 
for i in itertools.izip(*L4):
	     for j in i:
	            print j

1
4
7
2
5
8
3
6
9


Notice the difference in sequence. In this case, the output is the first item of each list (1, 4, 7), followed by the second item on each list (2, 5, 8), and so on. This is different from the sequence of the first list items (1, 2, 3) followed by second list items (4, 5, 6), and so on.

Conclusion

In this article, we have learned several simple ways to sequentially iterate over multiple lists in Python. Basically, there are two ways to do this. The first approach is when you need to process all items of one list before moving to the next one. The second approach is where you need to process the first item of each list then the second item of each list and so on. In the first case, you can use the itertools.chain() function, a for loop, or a star(*) operator. In the second case, you need to use the itertools.izip() function.

Data structure Library Python (language)

Opinions expressed by DZone contributors are their own.

Related

  • An AI-Driven Architecture for Autonomous Network Operations (NetOps)
  • Extracting Clean Excel Tables From PDFs Using Python + Docling
  • Regex in Action: Practical Examples for Python Programmers
  • Python and Open-Source Libraries for Efficient PDF Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook