Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8093

Python • Re: Iterating over pairs of list elements

$
0
0

Code:

from itertools import productfor px, opx in product(bodies, bodies[1:]):    print(px, opx)    # or if you want AA, AB, AC, Ba, BB, BC, ....for px, opx in product(bodies, repeat=2):    print(px, opx)
https://docs.python.org/3/library/itert ... ls.product

And if you want to have an index during iteration, you should use enumerate.
If you don't need an index, work with the objects directly, instead of using an index to access single objects in the sequence. This is a known antipattern and looks not pythonic.

Statistics: Posted by DeaD_EyE — Sun Feb 15, 2026 10:10 am



Viewing all articles
Browse latest Browse all 8093

Trending Articles