0

So I have some code as follows:

amount_of_episodes = len(episodes_from_user_show)
for x in episodes_from_user_show:
    print("[{0}] Episode {1}: {2}".format(amount_of_episodes, x.episode_number,
                                          x.name))  # Prints the available list of episodes.
    amount_of_episodes -= 1

What it has done is that it takes the length of a list, it iterates backwards until it reaches 0 and the episode name and number from the api I am using is also being printed.

So what I wanted to know is if there was a more readable way of doing this. Something like using enumerate that can display the size of the list going backwards but the content of the list going forwards (if that makes sense)?

Brandon Dodds
  • 93
  • 1
  • 1
  • 7
  • You could do `for i, x in enumerate(episodes_from_user_show)` and then `format(amount_of_episodes - i, ...)`. – ekhumoro Aug 18 '17 at 17:53

1 Answers1

1

A simple way to do this is to just print len(values) - index:

shows = [{'id': 1, 'name': 'show_1'},{'id': 2, 'name': 'show_2'}]
for idx, show in enumerate(shows):
    print("[{}] Episode {}: {}".format(len(shows) - idx, show['id'], show['name']))

output:

[2] Episode 1: show_1
[1] Episode 2: show_2
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70