snippetpythonCriticalCanonical
How do I iterate through two lists in parallel?
Viewed 0 times
parallelhowlistsiteratethroughtwo
Problem
I have two iterables, and I want to go over them in pairs:
That should result in:
One way to do it is to iterate over the indices:
But that seems somewhat unpythonic to me. Is there a better way to do it?
Related tasks:
* How to merge lists into a list of tuples? - given the above
* How can I make a dictionary (dict) from separate lists of keys and values? - create the dict
* Create a dictionary with comprehension - constructing
foo = [1, 2, 3]
bar = [4, 5, 6]
for (f, b) in iterate_together(foo, bar):
print("f:", f, " | b:", b)
That should result in:
f: 1 | b: 4
f: 2 | b: 5
f: 3 | b: 6
One way to do it is to iterate over the indices:
for i in range(len(foo)):
print("f:", foo[i], " | b:", bar[i])
But that seems somewhat unpythonic to me. Is there a better way to do it?
Related tasks:
* How to merge lists into a list of tuples? - given the above
foo and bar, create the list [(1, 4), (2, 5), (3, 6)].* How can I make a dictionary (dict) from separate lists of keys and values? - create the dict
{1: 4, 2: 5, 3: 6}.* Create a dictionary with comprehension - constructing
dict using zip in a dict comprehension.Solution
Python 3
In Python 3,
returns an iterator of tuples, like
of tuples, use
exhausted, you would use
itertools.zip_longest.
Python 2
In Python 2,
returns a list of tuples. This is fine when
temporary variable, and should be replaced by
When the shorter iterator(s) are exhausted,
Note also that
prints
for f, b in zip(foo, bar):
print(f, b)zip stops when the shorter of foo or bar stops.In Python 3,
zipreturns an iterator of tuples, like
itertools.izip in Python2. To get a listof tuples, use
list(zip(foo, bar)). And to zip until both iterators areexhausted, you would use
itertools.zip_longest.
Python 2
In Python 2,
zipreturns a list of tuples. This is fine when
foo and bar are not massive. If they are both massive then forming zip(foo,bar) is an unnecessarily massivetemporary variable, and should be replaced by
itertools.izip oritertools.izip_longest, which returns an iterator instead of a list.import itertools
for f,b in itertools.izip(foo,bar):
print(f,b)
for f,b in itertools.izip_longest(foo,bar):
print(f,b)izip stops when either foo or bar is exhausted.izip_longest stops when both foo and bar are exhausted.When the shorter iterator(s) are exhausted,
izip_longest yields a tuple with None in the position corresponding to that iterator. You can also set a different fillvalue besides None if you wish. See here for the full story.Note also that
zip and its zip-like brethen can accept an arbitrary number of iterables as arguments. For example,for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'],
['red', 'blue', 'green']):
print('{} {} {}'.format(num, color, cheese))prints
1 red manchego
2 blue stilton
3 green brieCode Snippets
for f, b in zip(foo, bar):
print(f, b)import itertools
for f,b in itertools.izip(foo,bar):
print(f,b)
for f,b in itertools.izip_longest(foo,bar):
print(f,b)for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'],
['red', 'blue', 'green']):
print('{} {} {}'.format(num, color, cheese))1 red manchego
2 blue stilton
3 green brieContext
Stack Overflow Q#1663807, score: 2020
Revisions (0)
No revisions yet.