Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple for loop iterators to unpack in Python [duplicate]

I am wondering if it's posiible to iterate over two lists at once.
Something like that:

for x, m in list1, list2:
    ...

I know that I should use the '.items()', but I don't want to create dictionary from two lists.
Any ideas?

like image 656
R.O.S.S Avatar asked Jun 18 '26 09:06

R.O.S.S


1 Answers

Use zip.

for x, m in zip(list1, list2):

zip(*iterables)

Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

like image 97
Avinash Raj Avatar answered Jun 20 '26 00:06

Avinash Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!