1554

Can I use list comprehension syntax to create a dictionary?

For example, by iterating over pairs of keys and values:

d = {... for k, v in zip(keys, values)}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 1
    Related: `collections.Counter` is a specialized type of dict for counting things: [Using a dictionary to count the items in a list](https://stackoverflow.com/questions/3496518/using-a-dictionary-to-count-the-items-in-a-list) – smci May 26 '20 at 23:09
  • 1
    If you weren't using `zip(...)` and using a separate dict, you'll need to do something like `d = {... for (k, v) in other_dict.items()}` – Miguel Vacas Mar 21 '23 at 01:43

17 Answers17

2292

Use a dict comprehension (Python 2.7 and later):

{key: value for key, value in zip(keys, values)}

Alternatively, use the dict constructor (for str keys only):

pairs = [('a', 1), ('b', 2)]
dict(pairs)                          # → {'a': 1, 'b': 2}
dict((k, v + 10) for k, v in pairs)  # → {'a': 11, 'b': 12}

Given separate lists of keys and values, use the dict constructor with zip:

keys = ['a', 'b']
values = [1, 2]
dict(zip(keys, values))              # → {'a': 1, 'b': 2}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
fortran
  • 74,053
  • 25
  • 135
  • 175
  • 1
    what if I have a case of list of words ['cat','dog','cat'] and I want to make a dict with key as word and value as count? Is there a short efficient syntax for that? – cryanbhu Jul 14 '19 at 02:30
  • 1
    @cryanbhu if what you mean is to count the repetitions of a given element in the list, there's a Counter class in the collections package: https://docs.python.org/2/library/collections.html#collections.Counter – fortran Jul 19 '19 at 05:52
291

In Python 3 and Python 2.7+, dictionary comprehensions look like the below:

d = {k:v for k, v in iterable}

For Python 2.6 or earlier, see fortran's answer.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
73

In fact, you don't even need to iterate over the iterable if it already comprehends some kind of mapping, the dict constructor doing it graciously for you:

>>> ts = [(1, 2), (3, 4), (5, 6)]
>>> dict(ts)
{1: 2, 3: 4, 5: 6}
>>> gen = ((i, i+1) for i in range(1, 6, 2))
>>> gen
<generator object <genexpr> at 0xb7201c5c>
>>> dict(gen)
{1: 2, 3: 4, 5: 6}
michaelmeyer
  • 7,985
  • 7
  • 30
  • 36
52

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}

You're looking for the phrase "dict comprehension" - it's actually:

mydict = {k: v for k, v in iterable}

Assuming blah blah blah is an iterable of two-tuples - you're so close. Let's create some "blahs" like that:

blahs = [('blah0', 'blah'), ('blah1', 'blah'), ('blah2', 'blah'), ('blah3', 'blah')]

Dict comprehension syntax:

Now the syntax here is the mapping part. What makes this a dict comprehension instead of a set comprehension (which is what your pseudo-code approximates) is the colon, : like below:

mydict = {k: v for k, v in blahs}

And we see that it worked, and should retain insertion order as-of Python 3.7:

>>> mydict
{'blah0': 'blah', 'blah1': 'blah', 'blah2': 'blah', 'blah3': 'blah'}

In Python 2 and up to 3.6, order was not guaranteed:

>>> mydict
{'blah0': 'blah', 'blah1': 'blah', 'blah3': 'blah', 'blah2': 'blah'}

Adding a Filter:

All comprehensions feature a mapping component and a filtering component that you can provide with arbitrary expressions.

So you can add a filter part to the end:

>>> mydict = {k: v for k, v in blahs if not int(k[-1]) % 2}
>>> mydict
{'blah0': 'blah', 'blah2': 'blah'}

Here we are just testing for if the last character is divisible by 2 to filter out data before mapping the keys and values.

Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
44

In Python 2.7, it goes like:

>>> list1, list2 = ['a', 'b', 'c'], [1,2,3]
>>> dict( zip( list1, list2))
{'a': 1, 'c': 3, 'b': 2}

Zip them!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sphynx-HenryAY
  • 746
  • 6
  • 10
33

Python version >= 2.7, do the below:

d = {i: True for i in [1,2,3]}

Python version < 2.7(RIP, 3 July 2010 - 31 December 2019), do the below:

d = dict((i,True) for i in [1,2,3])
Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
26

To add onto @fortran's answer, if you want to iterate over a list of keys key_list as well as a list of values value_list:

d = dict((key, value) for (key, value) in zip(key_list, value_list))

or

d = {key: value for (key, value) in zip(key_list, value_list)}
Igor Suhotin
  • 41
  • 1
  • 2
  • 5
Blairg23
  • 11,334
  • 6
  • 72
  • 72
7

Just to throw in another example. Imagine you have the following list:

nums = [4,2,2,1,3]

and you want to turn it into a dict where the key is the index and value is the element in the list. You can do so with the following line of code:

{index:nums[index] for index in range(0,len(nums))}
Ekhtiar
  • 943
  • 10
  • 9
  • The OP's question specified "For example, by iterating over pairs of keys and values", so most of the answers focus on that. However, I upvoted this because it is a good example for handling the list to dictionary use case. – Subfuzion Dec 07 '21 at 05:05
6

Here is another example of dictionary creation using dict comprehension:

What i am tring to do here is to create a alphabet dictionary where each pair; is the english letter and its corresponding position in english alphabet

>>> import string
>>> dict1 = {value: (int(key) + 1) for key, value in 
enumerate(list(string.ascii_lowercase))}
>>> dict1
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 
'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 
19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
>>> 

Notice the use of enumerate here to get a list of alphabets and their indexes in the list and swapping the alphabets and indices to generate the key value pair for dictionary

Hope it gives a good idea of dictionary comp to you and encourages you to use it more often to make your code compact

nomoreabond2017
  • 150
  • 2
  • 5
6

This code will create dictionary using list comprehension for multiple lists with different values that can be used for pd.DataFrame()

#Multiple lists 
model=['A', 'B', 'C', 'D']
launched=[1983,1984,1984,1984]
discontinued=[1986, 1985, 1984, 1986]

#Dictionary with list comprehension
keys=['model','launched','discontinued']
vals=[model, launched,discontinued]
data = {key:vals[n] for n, key in enumerate(keys)}

#Convert dict to dataframe
df=pd.DataFrame(data)
display(df)

enumerate will pass n to vals to match each key with its list

ASE
  • 1,702
  • 2
  • 21
  • 29
5

Try this,

def get_dic_from_two_lists(keys, values):
    return { keys[i] : values[i] for i in range(len(keys)) }

Assume we have two lists country and capital

country = ['India', 'Pakistan', 'China']
capital = ['New Delhi', 'Islamabad', 'Beijing']

Then create dictionary from the two lists:

print get_dic_from_two_lists(country, capital)

The output is like this,

{'Pakistan': 'Islamabad', 'China': 'Beijing', 'India': 'New Delhi'}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Savad KP
  • 1,625
  • 3
  • 28
  • 40
5

Adding to @Ekhtiar answer, if you want to make look up dict from list, you can use this:

names = ['a', 'b', 'd', 'f', 'c']
names_to_id = {v:k for k, v in enumerate(names)}
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'f': 4}

Or in rare case that you want to filter duplicate, use set first (best in list of number):

names = ['a', 'b', 'd', 'f', 'd', 'c']
sorted_list = list(set(names))
sorted_list.sort()
names_to_id = {v:k for k, v in enumerate(sorted_list)}
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'f': 4}

names = [1,2,5,5,6,2,1]
names_to_id = {v:k for k, v in enumerate(set(names))}
# {1: 0, 2: 1, 5: 2, 6: 3}
Muhammad Yasirroni
  • 1,512
  • 12
  • 22
  • v:k in the first codeblock is misleading, it reads like value:key which is the wrong way around to create a dict. I'd suggest `{name:index for index, name in enumerate(names)}` as an improvement. – bjrne Dec 23 '21 at 01:52
  • Also, you don't need the second part at all. Since it's a dict, it will by design remove duplicates by overwriting the entry. – bjrne Dec 23 '21 at 01:58
  • @bjrne I preserve the original answer on that and I don't feel it misleading at all. Nope, the second part is to make lookup dict. If you not use set, the index will not in order. It's rare case and I also give the output, so just use if that is your use case. – Muhammad Yasirroni Dec 23 '21 at 22:14
2
>>> {k: v**3 for (k, v) in zip(string.ascii_lowercase, range(26))}

Python supports dict comprehensions, which allow you to express the creation of dictionaries at runtime using a similarly concise syntax.

A dictionary comprehension takes the form {key: value for (key, value) in iterable}. This syntax was introduced in Python 3 and backported as far as Python 2.7, so you should be able to use it regardless of which version of Python you have installed.

A canonical example is taking two lists and creating a dictionary where the item at each position in the first list becomes a key and the item at the corresponding position in the second list becomes the value.

The zip function used inside this comprehension returns an iterator of tuples, where each element in the tuple is taken from the same position in each of the input iterables. In the example above, the returned iterator contains the tuples (“a”, 1), (“b”, 2), etc.

Output:

{'i': 512, 'e': 64, 'o': 2744, 'h': 343, 'l': 1331, 's': 5832, 'b': 1, 'w': 10648, 'c': 8, 'x': 12167, 'y': 13824, 't': 6859, 'p': 3375, 'd': 27, 'j': 729, 'a': 0, 'z': 15625, 'f': 125, 'q': 4096, 'u': 8000, 'n': 2197, 'm': 1728, 'r': 4913, 'k': 1000, 'g': 216, 'v': 9261}
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
1

Yes, it's possible. In python, Comprehension can be used in List, Set, Dictionary, etc. You can write it this way

mydict = {k:v for (k,v) in blah}

Another detailed example of Dictionary Comprehension with the Conditional Statement and Loop:

parents = [father, mother]
            
parents = {parent:1 - P["mutation"] if parent in two_genes else 0.5 if parent in one_gene else P["mutation"] for parent in parents}
MD. ABU SAYED
  • 241
  • 2
  • 5
0

You can create a new dict for each pair and merge it with the previous dict:

reduce(lambda p, q: {**p, **{q[0]: q[1]}}, bla bla bla, {})

Obviously this approaches requires reduce from functools.

Mahmoud
  • 9,729
  • 1
  • 36
  • 47
0

Assuming blah blah blah is a two-tuples list:

Let's see two methods:

# method 1
>>> lst = [('a', 2), ('b', 4), ('c', 6)]
>>> dict(lst)
{'a': 2, 'b': 4, 'c': 6}
# method 2
>>> lst = [('a', 2), ('b', 4), ('c', 6)]
>>> d = {k:v for k, v in lst}
>>> d
{'a': 2, 'b': 4, 'c': 6}
0

this approach uses iteration over the given date using a for loop.

Syntax: {key: value for (key, value) in data}

Eg:

# create a list comprehension with country and code:
    Country_code = [('China', 86), ('USA', 1),
            ('Ghana', 233), ('Uk', 44)]

# use iterable method to show results
{key: value for (key, value) in Country_code}
Kofi
  • 1,224
  • 1
  • 10
  • 21