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

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

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • Python Packages for Data Science
  • Unleashing the Power of Python: A Deep Dive into Data Visualization
  • Advantages of Python as an AI and ML Development Language
  • Python F-Strings

Trending

  • A Java developer's guide to Quarkus
  • When Not To Use Apache Kafka (Lightboard Video)
  • Strategies for Building Self-Healing Software Systems
  • Knowledge Graph Enlightenment, AI, and RAG
  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
Like (1)
Save
Tweet
Share
1.3K 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
  • Unleashing the Power of Python: A Deep Dive into Data Visualization
  • Advantages of Python as an AI and ML Development Language
  • Python F-Strings

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send 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: