In fact all_timezones
is a list of timezone names. If you want to create another list containing dictionaries (which in turn contain a sequential number and the name of the Timezone), just do:
timezones = []
for i, tz in enumerate(all_timezones):
timezones.append({ 'value': str(i + 1), 'text': tz })
With enumerate
you scroll through the list and its contents at the same time.
That said, I don’t know what the advantage of creating dictionaries value
is a sequential number, as lists already have their elements in sequential numerical indices (the difference is that it starts at zero instead of 1). If the idea is to search for an element by the numerical index, a list is much better for it (just do all_timezones[numero]
- recalling that the first element is at zero, the second at index 1, etc).
Another alternative is to use a comprehensilist on, much more succinct and pythonic:
timezones = [ { 'value': str(i + 1), 'text': tz } for i, tz in enumerate(all_timezones) ]
Thank you very much for all explanations, I will study on list comprehension, I agree your explanation regarding advantage. But as they created in the project this way I can not change.
– Mário Rodeghiero