How can we merge key value of dictionary?

aditya

I have dictionary

[{'a': 'tc1', 'b': 'tc2'}, {'a': 'tc1', 'b': 'tc3'}]

I want to create resultant dictionary

result = {'tc1':['tc2', 'tc3']}

Any idea how we can do that?

js = [{'a': 'tc1', 'b': 'tc2'}, {'a': 'tc1', 'b': 'tc3'}]
ls =[]
for i in js:
     x[i['a']]= ls+[(i["b"])]
francisco sollima

If I understood your question correctly, the 'a' element will always be the key for the new dictionary, and the 'b' element, its value. Then the following code should work for you:

js = [{'a': 'tc1', 'b': 'tc2'}, {'a': 'tc1', 'b': 'tc3'}]
result = {}  # Create the empty result dictionary
for i in js:
    k, v = i['a'], i['b']  # k will be the key, v will be the value
    if k not in result:  # If k is not a key of result yet, we create the entry as an empty list
        result[k] = []
    # By now, we know that k is a key in result. Now we add the value.
    result[k].append(v)
print result # {'tc1': ['tc2', 'tc3']}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In ruby how can we merge two given hash into single hash and replace duplicate key value with greater of both the value

how can we merge strings based on a key data

How to merge two Dictionary in python with same key but different value

How can I return the value of a key based on a different value in a dictionary

If value is equal with a key in the given dictionary, how can I append to the key of the said value the value of the first key

Can we to refer to a dictionary get a value from the key while replacing in Python?

How can one switch the value and key of a Nested dictionary in Python?

How can I update a graph in a Dictionary with right key/value?

How can I print value of a key in a dictionary as a list?

How can I add a key and its value to a dictionary?

How can I get key's value from dictionary in Swift?

How can I prevent adding the same value (With another key) into a dictionary?

How can I search the value of a key on a recursive dictionary?

How can I append each key and value of dictionary to columns?

How can I reverse just the key to a dictionary in python but not the value

How can I rename values in a dictionary with a corresponding key value?

How can I do reversed dictionary for every value and a key in python?

How can i merge array with the same key's value

How can I merge two dictionaries with multiple key value pairs

How can we check key exists in dictionary after converting data-type (dtype)?

How to access key by value in a dictionary?

How to change the key and value of a dictionary

How to change the key value of a dictionary?

How can I get the key of the object with a lowest attribute value for a Python dictionary with a string as key and a class object as value?

How can I append key,value pair to empty dictionary with the index value as the key pair?

How to change the value of a key in a dictionary which is in a dictionary

How to merge 2 arrays of equal length into a single dictionary with key:value pairs in Godot?

How do I merge 2 separate key value pairs inside the same dictionary?

How can we merge shapes in Java awt?

TOP Ranking

HotTag

Archive