I have two iterables in Python, and I want to go over them in pairs:
foo = (1, 2, 3) bar = (4, 5, 6) for (f, b) in some_iterator(foo, bar): print("f: ", f, "; b: ", b)
It 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?
list_1 = [1, 2, 3, 4] list_2 = ['a', 'b', 'c'] for i, j in zip (list_1, list_2): print(i, j) Using zip () method, you can iterate through two lists parallel as shown above. The loop runs until the shorter list stops (unless other conditions are passed).
Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step.
You can create a new thread and iterate your list there. Spawn multiple of this therad and you can iterate your list in parallel. If you want to pass List of any template type, you can just specify your method parameter as List, although this might result in compiler warnings.
I don't think you are really saying in parallel, since the values are being used to invoke a method. If you want the same elements from each list, concurrently skipping through different lists on different threads does you no good. You just need to do a for loop and then call list1.get (i), list2.get (i), list3.get (i).
You want the zip
function.
for (f,b) in zip(foo, bar): print "f: ", f ,"; b: ", b
for f, b in zip(foo, bar): print(f, b)
zip
stops when the shorter of foo
or bar
stops.
In Python 3, zip
returns an iterator of tuples, like itertools.izip
in Python2. To get a list of tuples, use list(zip(foo, bar))
. And to zip until both iterators are exhausted, you would use itertools.zip_longest.
In Python 2, zip
returns 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 massive temporary variable, and should be replaced by itertools.izip
or itertools.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 brie
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With