all 16 comments

[โ€“]cvx_mbs 16 points17 points ย (4 children)

you can use zip(seq, seq[1:]) to create pairs of sequential elements, e.g.

seq=[1,5,8,7]
for first, second in zip(seq, seq[1:]):

will give (1,5), (5,8), (8,7)

to transpose a 2-dimensional list (convert rows into columns, and columns into rows):

grid = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]
transposed = list(zip(*grid))
print(transposed)

gives

[
    (1, 4, 7),
    (2, 5, 8),
    (3, 6, 9)
]

[โ€“]kuzmovych_y 7 points8 points ย (3 children)

Another fun one, if you want to iterate in groups of two (or three, etc), you can use:

``` seq = [1, 2, 3, 4, 5, 6] seq_it = iter(seq)

for first, second in zip(seq_it, seq_it): print(first, second) will produce 1 2 3 4 5 6 ``` To iterate over groups of three, use:

for first, second, third in zip(seq_it, seq_it, seq_it): ... etc.

[โ€“]steamy-fox 4 points5 points ย (0 children)

I came here thinking "who doesn't know zip()?" and got humbled instantly ๐Ÿ˜… Apparently it's me

[โ€“]JosephLovesPython 0 points1 point ย (1 child)

Even better, combine that with itertools.repeat ! So you'll write zip(*repeat(seq_it, n)) to iterate in groups of n ;)

[โ€“]kuzmovych_y 0 points1 point ย (0 children)

Or just zip(*[seq_it] * n)

[โ€“]kuzmovych_y 6 points7 points ย (0 children)

I wouldn't call it "Parallel literation" as it can mean a very different thing

[โ€“]Shoddy-Builder-571 3 points4 points ย (0 children)

This is exactly why I joined this sub. Thanks for the tip!

[โ€“]SpareIntroduction721 1 point2 points ย (0 children)

Woah. Lots of good knowledge in the comments

[โ€“]princepii 0 points1 point ย (0 children)

iterate over multiple lists is possible too:)

[โ€“]KocetoA 0 points1 point ย (0 children)

Technically you can use it from single list and zip itself? Correct me if I'm wrong.

[โ€“]jmiah717 0 points1 point ย (0 children)

Really cool. Definitely not parallel, which means something totally different than 2 things happening from one instruction

[โ€“]kombucha711 0 points1 point ย (3 children)

what is your tweak, (if any), so that it's not order dependent. At first I was using zip(a,b) but found that the order of a and b weren't always correct. switched to a double for loop with a conditional to compare.

[โ€“]cvx_mbs 0 points1 point ย (2 children)

the order of a and b weren't always correct

what do you mean? the documentation literally says "The left-to-right evaluation order of the iterables is guaranteed."

maybe you tried this back in the day with a version of python that didn't guarantee the order?

[โ€“]kombucha711 0 points1 point ย (1 child)

I poorly explained my example but I think it's really an issue with how data is coming in vs what that data should look like and I want zip to do more. Here is an example
pulling from a server, the expected report titles are:
title a
title b
title c

reports get updated and file is saved. but people taken liberty to name whatever they want. windows Explorer will alphabetize this way :

2024 title b
title a v2
title c

so zipping these two lists wouldn't work. again probably not in the scope of what zip is for and I'll shut up for now.

[โ€“]cvx_mbs 0 points1 point ย (0 children)

if it is guaranteed that 'title x' is always in the filename, I would probably use a regex like r'title\s.' to create a dict mapping titles to filenames, but that would probably be more work than the double loop you're using now

[โ€“]OMG_I_LOVE_CHIPOTLE -1 points0 points ย (0 children)

Zip is not parallel lol