I want to generate the following effect :
for i, j in d.items() and k, v in c.items():
print i, j, k, v
This is wrong. I want to know how can I achieve this?
for (i, j), (k, v) in zip(d.items(), c.items()):
print i, j, k, v
Remember the order will be arbitrary unless your dictionaries are OrderedDict
s. To be memory efficient
In Python 2.x (where dict.items
and zip
create lists) you can do the following:
from itertools import izip
for (i, j), (k, v) in izip(d.iteritems(), c.iteritems()):
print i, j, k, v
This won't necessarily be faster, as you will observe on small lists it's faster to iterate over these intermediate lists however you will notice a speed improvement when iterating very large data structures.
In Python 3.x zip
is the same as izip
(which no longer exists) and dict.items
(dict.iteritems
no longer exists) returns a view instead of a list.
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