- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I iterate through two lists in parallel in Python?
Assuming that two lists may be of unequal length, parallel traversal over common indices can be done using for loop over over range of minimum length
>>> L1 ['a', 'b', 'c', 'd'] >>> L2 [4, 5, 6] >>> l=len(L1) if len(L1)<=len(L2)else len(L2) >>> l 3 >>> for i in range(l): print (L1[i], L2[i]) a 4 b 5 c 6
A more pythonic way is to use zip() function which results in an iterator that aggregates elements from each iterables
>>> for i,j in zip(L1,L2): print (i,j) a 4 b 5 c 6
- Related Articles
- How do I iterate through DOM elements in PHP?
- How we can iterate through a Python list of tuples?
- How to iterate through a dictionary in Python?
- How to iterate through a list in Python?
- How to iterate through a tuple in Python?
- How can I iterate over files in a given directory in Python?
- How to iterate two Lists or Arrays with one foreach statement in C#?
- How to compare two lists in Python?
- Python program to iterate over multiple lists simultaneously?
- Python - Ways to iterate tuple list of lists
- How to loop through multiple lists using Python?
- Dividing two lists in Python
- Iterate through ArrayList in Java
- How do we compare two lists in Python?
- How to correctly iterate through getElementsByClassName() in JavaScript?

Advertisements