Logo

Programming-Idioms

  • Go
  • Python

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.

x = enumerate(items)
print(*reversed([*x]))
for i, x in enumerate(reversed(items)):
  print(f'{i} {x}')
for i in range(len(items)-1, -1, -1):
    print(i, items[i])
import "fmt"
for i := len(items) - 1; i >= 0; i-- {
	x := items[i]
	fmt.Printf("Item %d = %v \n", i, x)
}
for(int i = items.Count - 1; i >= 0; i--)
{
    Console.WriteLine($"Index = {i}, Item = {items[i]}");
}

New implementation...
< >
programming-idioms.org