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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Python Packages for Data Science
  • Python and Open-Source Libraries for Efficient PDF Management
  • Essential Python Libraries: Introduction to NumPy and Pandas
  • Why Use AWS Lambda Layers? Advantages and Considerations

Trending

  • Navigating Change Management: A Guide for Engineers
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • Building an AI/ML Data Lake With Apache Iceberg
  • Advancing Robot Vision and Control
  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
5.9K 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

  • Python Packages for Data Science
  • Python and Open-Source Libraries for Efficient PDF Management
  • Essential Python Libraries: Introduction to NumPy and Pandas
  • Why Use AWS Lambda Layers? Advantages and Considerations

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!