Issue
I am on Python 3.9
What I have is two dictionaries.
dict1= {'ZS': [1, 2], 'ZP': [3], 'XS': [4, 5], 'XP': [6, 7]}
dict2= {'a': {}, 'b' : {}}
how do I take every two keys of dict1 and add them as dict-value in dict2.
Like below
required dict
result_dict= {'a': {'ZS': [1, 2], 'ZP': [3]}, 'b' : {'XS': [4, 5], 'XP': [6, 7]}}
Solution
You could do something like:
keys1 = list(dict1)
keys2 = list(dict2)
result_dict = {
keys2[i]: {
keys1[2*i]:dict1[keys1[2*i]],
keys1[2*i+1]:dict1[keys1[2*i+1]]
}
for i in range(len(keys2))
}
But in my opinion it is a really bad habit to trust dictionary key's order (unless you use OrderedDict
)
Answered By - TJR
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.