Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dictionary with list comprehension

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 
like image 220
flybywire Avatar asked Nov 17 '09 10:11

flybywire


People also ask

Can you use List Comprehension to make a dictionary?

Using dict() method we can convert list comprehension to the dictionary.

How do you create a dictionary in a list?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

How to create a dictionary with list comprehension in Python?

How to create a dictionary with list comprehension in Python? The zip () function which is an in-built function, provides a list of tuples containing elements at same indices from two lists. If two lists are keys and values respectively, this zip object can be used to constructed dictionary object using another built-in function dict ()

How to create a dictionary using simple expressions?

We can create dictionaries using simple expressions. Let’s see a example,lets assume we have two lists named keys and value now, We can use Dictionary comprehensions with if and else statements and with other expressions too. This example below maps the numbers to their cubes that are not divisible by 4:

What are Python dictionary comprehensions?

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

How to construct dictionary from ZIP object in Python?

If two lists are keys and values respectively, this zip object can be used to constructed dictionary object using another built-in function dict () In Python 3.x a dictionary comprehension syntax is also available to construct dictionary from zip object


2 Answers

Use a dict comprehension (Python 2.7 and later):

{key: value for (key, value) in iterable} 

Alternatively for simpler cases or earlier version of Python, use the dict constructor, e.g.:

pairs = [('a', 1), ('b', 2)] dict(pairs)                         #=> {'a': 1, 'b': 2} dict([(k, v+1) for k, v in pairs])  #=> {'a': 2, 'b': 3} 

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

keys = ['a', 'b'] values = [1, 2] dict(zip(keys, values))  #=> {'a': 1, 'b': 2} 
2) "zip'ped" from two separate iterables of keys/vals dict(zip(list_of_keys, list_of_values)) 
like image 154
fortran Avatar answered Oct 13 '22 12:10

fortran


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.

like image 41
SilentGhost Avatar answered Oct 13 '22 10:10

SilentGhost