Hacker News new | past | comments | ask | show | jobs | submit login

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:



And if you need the index:

    for idx, genre in enumerate (list_of_genres)


Out of curiosity, how is that dangerous?


My 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: