Use a dict comprehension (Python 2.7 and later):
Use with AI{key: value for key, value in zip(keys, values)}
Alternatively, use the dict constructor:
Use with AIpairs = [('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:
Use with AIkeys = ['a', 'b']
values = [1, 2]
dict(zip(keys, values)) # โ {'a': 1, 'b': 2}
Answer from fortran on Stack OverflowUse a dict comprehension (Python 2.7 and later):
Use with AI{key: value for key, value in zip(keys, values)}
Alternatively, use the dict constructor:
Use with AIpairs = [('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:
Use with AIkeys = ['a', 'b']
values = [1, 2]
dict(zip(keys, values)) # โ {'a': 1, 'b': 2}
In Python 3 and Python 2.7+, dictionary comprehensions look like the below:
Use with AId = {k:v for k, v in iterable}
For Python 2.6 or earlier, see fortran's answer.
Python list comprehension for dictionaries in dictionaries? - Stack Overflow
How to create a dictionary with "list comprehension"?
Create dictionary from list of dictionaries using list comprehension
Can you append to a dictionary with a list comprehension in Python?
What is the syntax for dictionary comprehension in Python?
The syntax for dictionary comprehension in Python is as follows: {key:value for (key,value) in iterable}
How do I create a dictionary from a list using dictionary comprehension in Python?
You can create a dictionary from a list using dictionary comprehension in Python by iterating over the list and setting the list elements as keys and values in the new dictionary.
What are the benefits of using dictionary comprehension in Python?
The benefits of using dictionary comprehension in Python are that it is concise, readable, and reduces the amount of code needed to create dictionaries. It also allows for filtering and transformation of dictionary elements in a single line of code.
You can do this:
Use with AIs = dict([ (k,r) for k,r in mydict.iteritems() if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ])
This takes a dict as you specified and returns a 'filtered' dict.
If dct is
Use with AI{'test1420': {'y': '060', 'x': '070', 'fname': 'test1420'},
'test277': {'y': 72, 'x': 94, 'fname': 'test277'},}
Perhaps you are looking for something like:
Use with AI[ subdct for key,subdct in dct.iteritems()
if 92<subdct['x']<95 and 70<subdct['y']<75 ]
A little nicety is that Python allows you to chain inequalities:
Use with AI92<dct[key]['x']<95
instead of
Use with AIif r['x'] > 92 and r['x'] < 95
Note also that above I've written a list comprehension, so you get back a list (in this case, of dicts).
In Python3 there are such things as dict comprehensions as well:
Use with AI{ n: n*n for n in range(5) } # dict comprehension
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
In Python2 the equivalent would be
Use with AIdict( (n,n*n) for n in range(5) )
I'm not sure if you are looking for a list of dicts or a dict of dicts, but if you understand the examples above, it is easy to modify my answer to get what you want.
I'm trying to create the following dictionary:
{ 0:3,0:4,0:5 }
And I did:
{ 0:i for i in range(3,6) }
Which doesn't work. Any idea how to? Maybe I can make it through a list of pairs? But is this way possible?
Original data:
list_of_dictionaries = [{'key1':'value1'},{'key2':'value2'},{'key3':['item1','item2','item3']}]Desired output
{'key1':'value1','key2':'value2','key3': ['item1','item2','item3']}Standart loop does the job, but i don't like it
empty_dictionary = {}
for item in list_of_dictionaries:
for k, v in item.items():
empty_dictionary[k] = vHow can i do it using list comprehension?