Print current and next element in a list, but in reverse order

Question:

Given a list = ["one", "two", "three"] I would like to print current and its next element, but in reverse order. That is:

three one
two three
one two

my script prints current and next element, but not in reverse order:

# It prints:
# one two
# two three
for curr, nxt in zip(list, list[1:]):
    print(curr, nxt)
  • How can I edit my script so that I achieve my goal?

I tried the following:

# It prints:
# three one
for curr, nxt in zip(list[-1:], list):
   print(curr, nxt)

but it only gives me one result.

Asked By: tail

||

Answers:

You can iterate the reversed index which will point you to the current item, and you can take modulo of the index by length of the list, which will point you to the next item

for i in range(len(lst), 0, -1):
    print(lst[i-1], lst[i%len(lst)])
  
# output:  
three one
two three
one two
Answered By: ThePyGuy

I guess you want this..

a = ["one", "two", "three"] 
a.append(a[0])
a.reverse()

for curr, nxt in zip(a, a[1:]):
    print(nxt, curr)

Also it is strongly recommended to avoid using keywords list as variable names.

Answered By: LianSheng

Assuming I understood your question correctly, the % operator will be helpful to find the next element if index exceeds size of list - 1.

  • Loop from index size-1 to 0
  • The next element is simply i+1. However to deal with the case where i+1 exceeds boundaries of the list, we can use (i+1)%size to "restart index".
lst = ["one", "two", "three"]
size= len(lst)

# loop starting at index (size-1) and ending at index  0
for i in range(size-1,-1,-1): 
    currentElement = lst[i]
    nextElement = lst[(i+1)%size] 
    print(currentElement, nextElement)

Output :

three one
two three
one two
Answered By: Bunny

Python’s zip is not the solution I would go for here, as you would need to concatenate two arrays in order to get the circularly shifted array (slice alone cannot shift circularly). What I would do however is simply run over all of the elements in the reversed list, and get the next value by it’s index, like so:

list = ["one", "two", "three"]

for i, curr in enumerate(list[::-1]): # enumerate gives you a generator of (index, value)
   nxt = list[-i] # list[-i-1] would be the current value, so -i would be the next one
   print(curr, nxt)

Edit:
Using list[::-1] is slightly slower than you would normally want it, because it would go over the list once to reverse it, and then another time to iterate over it. A better solution would be:

list = ["one", "two", "three"]

for i in range(len(list)-1, -1, -1):
    curr = list[i]
    nxt = list[len(list) - i - 1] # list[i+1] would not work as it would be index out of range, but this way it overflows to the negative side, which python allows.
    print(curr, nxt)

If you do however wish to use zip, you would need to do this:

list = ["one", "two", "three"]

for curr, nxt in zip(list[::-1], [list[0]] + list[:0:-1]):
    print(curr, nxt)

You should also note that naming your variable list is not a good idea as you would then shadow python’s built in list method, you should probably name it lst or something similar to that.

Answered By: Yuval.R
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.