4 ms·
A more Pythonic way do to this would be id_to_name = {g['id']: g['name'] for g in list_of_genres} And for i in range(len(list_of_genres)) is really
by aportnoy 8y ago
A more Pythonic way do to this would be
id_to_name = {g['id']: g['name'] for g in list_of_genres}
And
for i in range(len(list_of_genres))
is really a dangerous antipattern better replaced with
for genre in list_of_genres:
- stuartaxelowen 8y agoAnd if you need the index: for idx, genre in enumerate (list_of_genres)
- mattkrause 8y agoOut of curiosity, how is that dangerous?
- tonyarkles 8y agoMy guess is based on Python 2, where range(n) would return a fully inflated list of 0..(n-1). If len(x) is large, you could be allocating giant temporary lists just to iterate through them once. Using xrange() was the Python 2 solution (it would return a generator instead), but, if I recall correctly, Python 3 fixed this s.t. range() returns a generator.
- deleted 8y ago[deleted]