Asked • 05/24/19

Create a dictionary with list comprehension in Python?

I like the Python list comprehension syntax.Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values: mydict = {(k,v) for (k,v) in blah blah blah} # doesn't work

1 Expert Answer

By:

John L. answered • 05/24/19

Tutor
4.9 (221)

Machine Learning Professor and More

John L.

Oh, and I almost forgot, there's a shortcut for the above: "myDict = dict(zip(keys, values))" does exactly the same thing, even though it doesn't look like the list comprehension syntax.
Report

05/24/19

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.