provided that I have two lists in same length, list_a, list_b.
I can print they items in a single for loop as follows:
for i in range(0, len(list_a)):
print "%s %s" % (list_a[i], list_b[i])
is there any alternative and elegant way to do above mentioned task ?
I have tried
for a, b in list_a, list_b:
print ""
You need zip():
for a, b in zip(list_a, list_b):
# whatever
When the lists are long and you are using Python 2.x, you might prefer itertools.izip() to save some memory.
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