Swapping keys and dictionaries without loosing the data in python | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
- 2

Swapping keys and dictionaries without loosing the data in python

Geethika

15th Jun 2021, 6:53 AM
Ninja
Ninja - avatar
6 ответов
+ 3
Do you mean "swapping keys and values in dictionary"? If that is the case, then you can use for loop and iterate each key and value pair using dict.items() method which will return a tuple pair of key and value. dict.items() --> (key, value) Then create a different variable to store the new dictionary wherein the value and key are swapped. _______________________ data = {1: "one", 2: "two"} new_data = { } for key, value in data.items(): new_data[value] = key _______________________ Or use list comprehension-like for loop. ________________________ data = {1: "one", 2: "two"} new_data = {val: key for key, val in data.items()} ________________________ If this is not what you are asking for, please let me know. Thanks.
15th Jun 2021, 7:06 AM
noteve
noteve - avatar
+ 3
Just a little adjustment, this one checks first if a value is already a key in new_d, if not create one with an empty list as its predetermined value, then append the key of data as an element of its list value. d = {"Apple":"fruit", "orange":"fruit", "lilly":"flower", "rose":"flower"} new_d = { } for key, value in d.items(): if value not in new_d: new_d[value] = [ ] new_d[value].append(key) Still not sure if this is what you are looking for, so again please let me know if this is not. Thanks.
15th Jun 2021, 7:27 AM
noteve
noteve - avatar
+ 1
My question is not to loose the duplicate data for example:#Swap the dict keys and values but should not miss the data d = {"Apple":"fruit","orange":"fruit","lilly":"flower","rose":"flower"} #output: {'fruit': ['orange'], 'flower': ['rose']} and another output should be in #output: {'fruit': ['orange','Apple'], 'flower': ['rose',"rose"]}
15th Jun 2021, 7:14 AM
Ninja
Ninja - avatar
+ 1
This is what i looking for tq
15th Jun 2021, 7:55 AM
Ninja
Ninja - avatar
+ 1
Can u explain once
15th Jun 2021, 7:58 AM
Ninja
Ninja - avatar
0
542-geethika kodidela [ 1 ] First, the for loop iterates the key and value by unpacking the d.items() key-value pair. d.items() --> [("Apple", "fruit"), ("orange", "fruit"), ("lilly", "flower"), ("rose", "flower")] And in the for loop we used two variables separated by comma. 🔹 for key, value in d.items(): Inside the for loop, on the first pair for example, the `key` represents "Apple" and `value` represents "fruit". It is an easy way to iterate each key and value (pair) of a dictionary at the same time. [ 2 ] Since we are using the value as key and key as value (i.e. swapping), we check first if `value` is already a key in `new_d` dictionary, if not yet, then we need to add that `value` as key and add an empty list so we can append elements to it afterwards. Then at the 3rd line of the for loop, it adds the `key` to the list of the value of the `value` key in new_d. As a result, whichever key in `d` have the same value will be in the same list holded by that same value.
15th Jun 2021, 8:08 AM
noteve
noteve - avatar