Python: How to iterate list in reverse order

时间:2023-01-23 22:48:03

#1

for index, val in enumerate(reversed(list)):

  print len(list) - index - 1, val

 

#2 

def reverse_enum(L):
  for index in reversed(xrange(len(L))):
    yield index, L[index]

L = ['foo', 'bar', 'bas']
for index, item in reverse_enum(L):
  print index, item

 

#3

L = ['foo', 'bar', 'bas']

for index in reversed(range(len(L))):
  print index, L[index]