Home > Blockchain >  zip object is not reversible when trying to reverse zip result
zip object is not reversible when trying to reverse zip result

Time:09-16

This is my script:

list = ["namaste", "hello", "ciao", "salut"]

# Ciao, Salut
# Hello, Ciao
# Namaste, Hello
for el, succ in reversed(zip(list, list[1:]))
    print(el, succ)

unfortunately I am getting:

zip is not reversible

I would like to get the results you see in the comments.

  • How can I do?

CodePudding user response:

You can use zip:

l = ["Namaste", "Hello", "Ciao", "Salut"]

for a,b in zip(l[-2::-1], l[::-1]):
    print(f'{a}, {b}')

output:

Ciao, Salut
Hello, Ciao
Namaste, Hello

CodePudding user response:

If you're using python 3.10, you could make use of itertools.pairwise(), like this:

from itertools import pairwise

for a, b in pairwise(["Namaste", "Hello", "Ciao", "Salut"]):
    print(a, b)

this prints:

Namaste Hello
Hello Ciao
Ciao Salut
  • Related