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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • 6 Techniques To Reduce LLM API Costs With the Python Library
  • An AI-Driven Architecture for Autonomous Network Operations (NetOps)
  • Extracting Clean Excel Tables From PDFs Using Python + Docling

Trending

  • Kafka vs Flink vs Spark: Do You Really Need Real-Time?
  • Agent Harness, Memory Indexing, and AI Governance in Test Automation
  • How to Hide Sensitive Information Before You Share a File
  • Building a Feature Store With Neo4j and Feast

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 20, 24 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • 6 Techniques To Reduce LLM API Costs With the Python Library
  • An AI-Driven Architecture for Autonomous Network Operations (NetOps)
  • Extracting Clean Excel Tables From PDFs Using Python + Docling

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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