Adding keys and values to a dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Adding keys and values to a dictionary (/thread-31044.html) |
Adding keys and values to a dictionary - giladal - Nov-19-2020 Hey Guys. Trying to add value, key in this order to a dictionary. Checking with the debugger it shows that index 0 is added to new_dict, and then in the second iteration, index 1 replaces index 0 and then index 2 is added correctly. Any idea why? course_dict = {'I': 3, 'love': 3, 'self.py!': 2} def inverse_dict(my_dict): new_dict = {} for key, value in my_dict.items(): new_dict.update({value: key}) return new_dict print(inverse_dict(course_dict)) RE: Adding keys and values to a dictionary - bowlofred - Nov-19-2020 Keys in a dictionary are unique, but the starting values you have are not unique. So simply reversing the order of values and keys gives a key collision. What do you think should be the answer in your final dictionary if you asked it for the key 3? inverse_dict[3] == ? If you didn't want to lose data, you could make the value a container, like a set or a list and append any conflicts together. RE: Adding keys and values to a dictionary - giladal - Nov-19-2020 (Nov-19-2020, 04:39 PM)bowlofred Wrote: Keys in a dictionary are unique, but the starting values you have are not unique. So simply reversing the order of values and keys gives a key collision. What do you think should be the answer in your final dictionary if you asked it for the key 3? Thanks for your quick reply. You are right, I got the exercise wrong. It involves making a list. Ill give it a go now. Thanks again! RE: Adding keys and values to a dictionary - deanhystad - Nov-19-2020 This is the dictionary you are trying to make: Lets see what happens if we try to create the dictionary from a literal.inverse_dict = {3:'I', 3:'love', 2:'self.py!'} print(inverse_dict) Hmmm, same problem.Lets try building the dictionary step by step: inverse_dict = {} inverse_dict[3] = 'I' inverse_dict[3] = 'love' inverse_dict[2] = 'self.py!' print(inverse_dict) Hopefully this last example makes it clear why 'love' replaces 'I' when you SET inverse_dict[3] = 'love'. The keys in a dictionary are unique. When you set the key:value in a dictionary it sets the value associated with the key. If the key does not exist the key is added.This is a potential problem with inverting a dictionary. A dictionary does not care if the values are unique, only the keys. When you swap keys and values the dictionary will happily overwrite any duplicate values. You could make this a key error. course_dict = {'I': 3, 'love': 3, 'self.py!': 2} def inverse_dict(my_dict): new_dict = {} for key, value in my_dict.items(): if value in new_dict: raise KeyError(f'Key {value} already exists') new_dict[value] = key return new_dict print(inverse_dict(course_dict))
|