Two dictionary is below
d1 = {'1': {'index': '1', 'sc': '4', 'st': '3'}, '2': {'index': '2', 'sc': '5', 'st': '5'}}
d2 = {'1': {'diff': 1}, '2': {'diff': 0}}
Code is below
z = {**d2, **d1} why this is not working
Tried below code also
def Merge(d1, d2):
return(d2.update(d1))
print(Merge(d1, d2))
Expetec out below
{'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1},
'2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
an alternate way using pandas
>>> import pandas as pd
>>> df = pd.DataFrame(d1)
>>> df2 = pd.DataFrame(d2)
>>> merged_dict = pd.concat([df,df2]).to_dict()
output
>>> merged_dict
{'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1}, '2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
generally, ** will capture any keyword arguments we pass to the function into a dictionary which that attributes arguments will reference. For example:
d1={'a':1,'b':2}
d2={'c':3,'d':4}
def merge(**di):
res = {}
for k, v in di.items():
try:
res[k].append(v)
except KeyError:
res[k] = [v]
return res
print(merge(**d1, **d2))
# {'a': [1], 'b': [2], 'c': [3], 'd': [4]}
However, if we pass in two dictionary with same keys:
d1 = {'1': {'index': '1', 'sc': '4', 'st': '3'}, '2': {'index': '2', 'sc': '5', 'st': '5'}}
d2 = {'1': {'diff': 1}, '2': {'diff': 0}}
def merge(**di):
res = {}
for k, v in di.items():
try:
res[k].append(v)
except KeyError:
res[k] = [v]
return res
print(merge(**d1, **d2))
# TypeError: merge() got multiple values for keyword argument '1'
This error is handled by continuing which keep the original one and skip the second dict key. Sorry I don't have a shorthand method for this.
d1 = {'1': {'index': '1', 'sc': '4', 'st': '3'}, '2': {'index': '2', 'sc': '5', 'st': '5'}}
d2 = {'1': {'diff': 1}, '2': {'diff': 0}}
def merge(*args):
res = {}
for di in args:
for k, v in di.items():
try:
res[k].update(v)
except KeyError:
res[k] = v
return res
print(merge(d1, d2))
# {'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1}, '2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
z = {**d2, **d1}
Overwrites everything in d2 with d1 values for keys '1', and '2'. It is tricky to merge dictionaries with the same keys so you don't overwrite key:value pairs within those keys.
The following will get you to the depth needed in both d1 and d2 to update d1 to your expected output:
d1['1']['diff'] = d2['1']['diff']
d1['2']['diff'] = d2['2']['diff']
print ('d1:', d1)
Output:
d1: {'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1}, '2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
>>> for key in d1:
... d1[key].update(d2[key])
>>> d1
{'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1}, '2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
Update:
If you want in another identifier d3.
d3 = d1.copy()
for key in d3:
d3[key].update(d2[key])
print(d3)
Dictionaries are mutable objects. update function just mutates/updates the object and return None. So, you need to create a copy (so you have another object) and change the new object, if you want initial data unaltered.
Related
I have dictionary below
d = {'1': {'index': '1', 'sc': '4', 'st': '3'},
'2': {'index': '2', 'sc': '5', 'st': '5'}}
I need to create a new key diff with subtract sc - st
expected out
d1 = {'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1},
'2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
d2 = {'1':{'diff': 1}, '2': {'diff': 0}}
Pseudo code
from functools import reduce
d1 = {}
d2 = {}
for i,j in m.items():
#print (j)
for a,b in j.items():
#print (b)
d1['diff'] = reduce(lambda x,y: x-y, [int(i) for i in d.values()])
You can try
d1 = {k: {**v, "diff": int(v['sc']) - int(v['st'])} for k, v in d.items()}
d2 = {k: {"diff": int(v['sc']) - int(v['st'])} for k, v in d.items()}
Result
d1 = {'1': {'index': '1', 'sc': '4', 'st': '3', 'diff': 1}, '2': {'index': '2', 'sc': '5', 'st': '5', 'diff': 0}}
d2 = {'1': {'diff': 1}, '2': {'diff': 0}}
I have Dictionary is below, My product output is below. I need to create a new dictionary with two types Out_1, Out_2
product = {'Product1': {'index': '1', '1': 'Book', '2': 'Pencil', '3': 'Pen','value': '1'},
'Product2': {'index': '2', '1': 'Marker', '2': 'MYSQL', '3': 'Scale','value': '0'}}
If value inside product is 0 then extract the keys
Expected Output
Out_1 = {'Product2': {1:'Marker': '2': 'Compass', '3': 'Scale', 'value': 0}}
Out_2 = {'Product2':['Marker','Compass','Scale', '0']}
Psuedo code is below. i tried to create but not able to create as above
Out_1 = {}
Out_2 = {i:[]}
for i,j in product.items():
for a,b in j.items():
if a['value'] == 0:
Out_2.append(i)
I am getting indices error, How to get Out_1, Out_2
You can use dict comprehensions for this.
out_1 = {k: v for k, v in product.items() if v['value']=='0'}
out_2 = {k: list(v.values()) for k, v in product.items() if v['value']=='0'}
Hi do you really need this index variable? If not yes why would not you use list of dicts instead of dict of dicts. However here is what you wanted:
products = {'Product1': {'index': '1', '1': 'Book', '2': 'Pencil', '3': 'Pen','value': '1'},
'Product2': {'index': '2', '1': 'Marker', '2': 'MYSQL', '3': 'Scale','value': '0'}}
for k,product in products.items():
product.pop('index', None)
if product['value'] == '0':
products[k] = list(product.values())
print(products)
>>> {'Product1': {'1': 'Book', '2': 'Pencil', '3': 'Pen', 'value': '1'}, 'Product2': ['Marker', 'MYSQL', 'Scale', '0']}
I was not assigning it to any other variables like out1/2 in case you have more than 2 products
Here it is:
Code:
product = {'Product1': {'index': '1', '1': 'Book', '2': 'Pencil', '3': 'Pen','value': '1'},
'Product2': {'index': '2', '1': 'Marker', '2': 'MYSQL', '3': 'Scale','value': '0'}}
output_1 = {}
output_2 = {}
for key,val in product.items():
if (val['value'] == '0'):
output_1[key]=val
output_2[key]=val.values()
print(output_1)
print(output_2)
Output:
{'Product2': {'1': 'Marker', 'index': '2', '3': 'Scale', '2': 'MYSQL', 'value': '0'}}
{'Product2': ['Marker', '2', 'Scale', 'MYSQL', '0']}
This question already has answers here:
Combining lists of dictionaries based on list index
(2 answers)
Closed 3 years ago.
I having an issue of finding a way to merge these three lists into one in Python3.
list1 = [{'a': '1'}, {'a': '2'}, {'a': '3'}]
list2 = [{'b': '4'}, {'b': '5'}, {'b': '6'}]
list3 = [{'c': '7'}, {'c': '8'}, {'c': '9'}]
What I would like to do is getting a new list such as:
list4 = [{'a': '1', 'b': '4', 'c': '7'}, {'a': '2', 'b': '5', 'c': '8'}, {'a': '3', 'b': '6', 'c': '9'}]
Is there any Pythonic way to do this operation?
Use zip in a combined list- and dict comprehension:
>>> list1 = [{'a': '1'}, {'a': '2'}, {'a': '3'}]
>>> list2 = [{'b': '4'}, {'b': '5'}, {'b': '6'}]
>>> list3 = [{'c': '7'}, {'c': '8'}, {'c': '9'}]
>>> [{k: d[k] for d in ds for k in d} for ds in zip(list1, list2, list3)]
[{'a': '1', 'b': '4', 'c': '7'},
{'a': '2', 'b': '5', 'c': '8'},
{'a': '3', 'b': '6', 'c': '9'}]
Also works if the dicts contain more than one value. If the dicts in one "column" contain the same keys, the values in the later lists will overwrite the former.
This question already has answers here:
List of dicts to/from dict of lists
(14 answers)
Closed 4 years ago.
so given a list of dictionaries:
my_dict= [{'a': '4', 'b': '5', 'c': '1', 'd': '3'},
{'a': '1', 'b': '8', 'c': '1', 'd': '2'},
{'a': '7', 'b': '4', 'c': '1', 'd': '5'}]
and a list of keys in the dictionary for example [ 'a', 'b']
I am trying to make a list of dictionaries for both 'a' and 'b' for their respective values i.e the final product will resemble
new_dict = ['a':['4', '1', '7'], 'b':['5', '8', '4']]
any help will be appreciated
Using collections
Demo:
import collections
d = collections.defaultdict(list)
my_dict= [{'a': '4', 'b': '5', 'c': '1', 'd': '3'}, {'a': '1', 'b': '8', 'c': '1', 'd': '2'}, {'a': '7', 'b': '4', 'c': '1', 'd': '5'}]
for i in my_dict:
for k,v in i.items():
d[k].append(v)
print( d )
Output:
defaultdict(<type 'list'>, {'a': ['4', '1', '7'], 'c': ['1', '1', '1'], 'b': ['5', '8', '4'], 'd': ['3', '2', '5']})
I have a list of dictionaries -
list1 = [{'id' : '1', 'b' : '2', 'c' : '3'}, {'id' : '4', 'b' : '5', 'c' : '6'}, {'id' : '7', 'b' : '8', 'c' : ''}]
Based on the value of c being null or not, I am making a call which returns -
list2 - {'d' : '30', 'id' : 1}, {'d': '25', 'id' : '4'}
Now I want to modify list1, so that the final list has the values of d for the ids which have c. For example -
list1 = [{'id' : '1', 'b' : '2', 'c' : '3', 'd' : '30'}, {'id' : '4', 'b' : '5', 'c' : '6', 'd' : '25'}, {'id' : '7', 'b' : '8', 'c' : ''}]
My approach -
for l in list2:
current_list = {}
for l2 in list1:
if l2['id'] == l['id']:
current_list = l2
break
if current_list:
current_list['d'] = l['d']
Here the actual dict is not getting modified. How can I modify the actual list? Also, is there a neater way to do this?
I'm not certain I understand what you are trying to accomplish. Your written description of your goal does not agree with you code. Based on the code, I'm guessing that you want to match up the data based on the id values.
# You've got some dicts.
dicts = [
{'id': '1', 'b': '2', 'c': '3'},
{'id': '4', 'b': '5', 'c': '6'},
{'id': '7', 'b': '8', 'c': ''},
]
# You've got some other dicts having the same IDs.
d_dicts = [
{'d': '30', 'id': '1'},
{'d': '25', 'id': '4'},
]
# Reorganize that data into a dict, keyed by ID.
dlookup = {d['id'] : d['d'] for d in d_dicts}
# Now add that lookup data to the main list of dicts.
for d in dicts:
i = d['id']
if i in dlookup:
d['d'] = dlookup[i]
Assuming mr FMc are correct, there is in python 3.5 a valid approach to merge dicts. Which in this case would led us to:
dicts = [
{'id': '1', 'b': '2', 'c': '3'},
{'id': '4', 'b': '5', 'c': '6'},
{'id': '7', 'b': '8', 'c': ''},
]
d_dicts = [
{'d': '30', 'id': '1'},
{'d': '25', 'id': '4'},
]
dicts = [{**d, **dict(*filter(lambda x: x["id"] == d["id"], d_dicts))} for d in dicts]
I like these kinda expressions instead of writing it all out, but it has the "benefit" of crashing instead of overwriting stuff when there is more then one dict with the same id. But my solution still overwrites values if there are duplicate keys silently. The inserted value being the value from whatever's second in the dict merge.