

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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?
- Iterate through ArrayList in Java
- How can I iterate over files in a given directory in Python?
- How to loop through multiple lists using Python?
- How to iterate through Java List?
- How to iterate two Lists or Arrays with one foreach statement in C#?
- Dividing two lists in Python
- Iterate through Quartet class in JavaTuples
- Iterate through Triplet class in JavaTuples
- Iterate through Septet class in JavaTuples
- Iterate through Decade Tuple in Java
Advertisements