Search code examples
pythonperformancedictionary

Is there any benefit of using a dictionary comprehension if an equivalent dict(zip(a, b)) is possible?


Recently I have been working with dictionaries and I was to told to make dictionaries out of two lists like this:

zipped = {key: value for key, value in zip(drinks, caffeine)}

Later I forgot how to do that and found a different way that seems simpler to me:

zipped = dict(zip(drinks, caffeine))

Is there something worse about using this second example instead of the first?


Solution

  • Dictionary/List comprehensions are great flexible tools. You were taught this way so that the solution was more flexible and aligns with current coding practices. It's a "skill issue" as they say. There's nothing wrong with either. Always read company coding guidelines where possible.