Because I have just set out to program in Python at work (in depth), I am going to write down on my blog those interesting things that I have to work out related to this activity.
And for example, this week I had to cope a curious problem about merging dictionaries with any kind of element within, such as lists or other dictionaries. Browsing the Internet, I was able to find some functions to combine dictionaries, but the problem was that those methods did not take into account the option of having lists inside.
So in order to get over this issue, I had to develop the following two recursive methods.
As you can see in the above code, the method merge_dict loops through the dictionary and if it comes across a list, it will have a function able to merge the list. Also say that dict2 and list2 will have more priority that dict1 and list1 respectively.
Let's see an example where there are a couple of dictionaries with several elements within.
This is the output for the preceding script.
And for example, this week I had to cope a curious problem about merging dictionaries with any kind of element within, such as lists or other dictionaries. Browsing the Internet, I was able to find some functions to combine dictionaries, but the problem was that those methods did not take into account the option of having lists inside.
So in order to get over this issue, I had to develop the following two recursive methods.
defmerge_dict(dict1, dict2):
ifisinstance(dict1, dict) andisinstance(dict2, dict):
for k, v in dict2.iteritems():
if k notin dict1:
dict1[k] = v
else:
dict1[k] = merge_dict(dict1[k], v)
elifisinstance(dict1, list) andisinstance(dict2, list):
dict1 = merge_list(dict1, dict2)
elif dict2 ==None:
return dict1
else:
return dict2
return dict1
defmerge_list(list1, list2):
ifisinstance(list1, list) andisinstance(list2, list):
for i, item inenumerate(list2):
iflen(list1) > i:
ifisinstance(list1[i], dict) andisinstance(item, dict):
list1[i] = merge_dict(list1[i], item)
elifisinstance(list1[i], list) andisinstance(item, list):
list1[i] = merge_list(list1[i], item)
else:
list1[i] = item
else:
list1.append(list2[i])
elifisinstance(list1, dict) andisinstance(list2, dict):
list1 = merge_dict(list1, list2)
else:
return list2
return list1
As you can see in the above code, the method merge_dict loops through the dictionary and if it comes across a list, it will have a function able to merge the list. Also say that dict2 and list2 will have more priority that dict1 and list1 respectively.
Let's see an example where there are a couple of dictionaries with several elements within.
dict1 = {"country": [{"name": "Spain", "capital": "Madrid"}, {"name": "France"}]}
dict2 = {"country": [{"name": "Germany"}], "continent": "Europe"}
print merge_dict(dict1, dict2)
This is the output for the preceding script.
$ python test.py
{'country': [{'name': 'Germany', 'capital': 'Madrid'}, {'name': 'France'}], 'continent': 'Europe'}