This question already has answers here:
Python Dictionary Comprehension [duplicate]
(9 answers)
Closed 8 months ago.
Is there a shorter way to perform an operation on each dictionary item, returning a new dictionary without having to first create an empty dictionary like i did?
Note: The original a_dictionary values should not be changed.
a_dictionary = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
result_dict = {}
result_dict.update((x, y*2) for x, y in a_dictionary.items())
You can use dict comprehension:
result_dict = {x: y*2 for x, y in a_dictionary.items()}
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed last year.
I have like 10 dictionaries and I want to print all the dictionaries with all the keys inside.
Dict1 = {}
...
Dict10 = {}
We could do:
print(list(Dict1.keys()))
...
print(list(Dict10.keys()))
But is there a code more simple to do this? Like a for loop?
I'm working on a project where:
Operator types get_report
The program looks for all dictionaries in another .py file and print all the keys from every dictionary
In python globals() store all variable as a dictionary
dict1 = {1: 1, 2: 2}
dict2 = {6: 1, 7: 2}
dict3 = {4: 1, 3: 2}
var = globals()
for i in range(1, 11):
dict_name = f"dict{i}"
if var.get(dict_name):
keys = var[dict_name].keys()
print(list(keys))
#timegb suggested this might get crashed. So better approach to use dict.get to handle unknown keys
This question already has answers here:
How can one make a dictionary with duplicate keys in Python?
(9 answers)
Closed 3 years ago.
I would like to append to a dictionary for e.g
a={1:2}
a.update({2:4})
a.update({1,3})
I would like this to give
a ={1: (2,3), 2: 4}
How can this be done? There is no append. Update overwrites this as
{1: 3, 2: 4}
Please let me know
You could try something like this:
a = {1:2}
updates = [{2:4},{1:3}]
for dct in updates:
for key in dct:
if key in a:
a[key] = (a[key], dct[key])
else:
a[key] = (dct[key])
print(a)
#Gives: {1: (2, 3), 2: 4}
This question already has answers here:
How do I extract all the values of a specific key from a list of dictionaries?
(3 answers)
Closed 4 years ago.
I need to get a list from the dictionaries which is in a list
I tried [v for k,v in l[i].iteritems() for i in len(l) if k=='cell']
Example:
l = [{'cell':4,'num':55}, {'cell':5,'num':66}, {'cell':6,'num':77}]
I want to write a nested for loop in a single line and get output as
output = [4,5,6]
l = [{'cell': 4, 'num': 55}, {'cell': 5, 'num': 66}, {'cell': 6, 'num': 77}]
ls = []
for i in l:
ls.append(i['cell'])
print(ls)
Try this
it should work like this:
l = [{'cell':4,'num':55}, {'cell':5,'num':66}, {'cell':6,'num':77}]
list_new=[]
for i in l:
list_new.append(i['cell'])
This question already has answers here:
Extract a subset of key-value pairs from dictionary?
(14 answers)
Filter dict to contain only certain keys?
(22 answers)
Closed 5 years ago.
Dictionary:
d = {'a':[2,3,4,5],
'b':[1,2,3,4],
'c':[5,6,7,8],
'd':[4,2,7,1]}
I want to have d_new which containes only b and c items.
d_new = {'b':[1,2,3,4],
'c':[5,6,7,8]}
I want a scalable solution
EDIT:
I also need a method to create a new dictionary by numbers of items:
d_new_from_0_to_2 = {'a':[2,3,4,5],
'b':[1,2,3,4]}
If you want a general way to pick particular keys (and their values) from a dict, you can do something like this:
d = {'a':[2,3,4,5],
'b':[1,2,3,4],
'c':[5,6,7,8],
'd':[4,2,7,1]}
selected_keys = ['a','b']
new_d = { k: d[k] for k in selected_keys }
Gives:
{'a': [2, 3, 4, 5], 'b': [1, 2, 3, 4]}
I think that in Python 2.6 and earlier you would not be able to use a dict comprehension, so you would have to use:
new_d = dict((k,d[k]) for k in selected_keys)
Is this what you want?
new_d = dict(b=d.get('b'), c=d.get('c'))
This question already has answers here:
How to sort objects by multiple keys?
(8 answers)
Closed 7 years ago.
Suppose I have a coordinate list as the following:
list_ = [{'x':1,'y':2},{'x':1,'y':1},{'x':3,'y':3}]
I want to first sort x key, and then sort y key. So I expect:
list_ = [{'x':1,'y':1},{'x':1,'y':2},{'x':3:'y':3}]
How do I do?
Use the key parameter of sort/sorted. You pass a function that returns the key to sort upon. operator.itemgetter is an efficient way to generate the function:
>>> from operator import itemgetter
>>> list_ = [{'x':1,'y':2},{'x':1,'y':1},{'x':3,'y':3}]
>>> sorted(list_,key=itemgetter('x','y'))
[{'x': 1, 'y': 1}, {'x': 1, 'y': 2}, {'x': 3, 'y': 3}]