python - Enumerate the size of a list backwards, but keep the list going forwards -
so have code follows:
amount_of_episodes = len(episodes_from_user_show) x in episodes_from_user_show: print("[{0}] episode {1}: {2}".format(amount_of_episodes, x.episode_number, x.name)) # prints available list of episodes. amount_of_episodes -= 1 what has done takes length of list, iterates backwards until reaches 0 , episode name , number api using being printed.
so wanted know if there more readable way of doing this. using enumerate can display size of list going backwards content of list going forwards (if makes sense)?
a simple way print len(values) - index:
shows = [{'id': 1, 'name': 'show_1'},{'id': 2, 'name': 'show_2'}] 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
Comments
Post a Comment