Python Forum
Adding keys and values to a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding keys and values to a dictionary
#4
This is the dictionary you are trying to make:
Output:
{3:'I', 3:'love', '2':'self.py!'}
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)
Output:
{3: 'love', '2': 'self.py!'}
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)
Output:
{3: 'love', '2': 'self.py!'}
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))
Output:
Traceback (most recent call last): File "...", line 14, in <module> print(inverse_dict(course_dict)) File "...", line 8, in inverse_dict raise KeyError(f'Key {value} already exists') KeyError: 'Key 3 already exists'
Reply


Messages In This Thread
Adding keys and values to a dictionary - by giladal - Nov-19-2020, 03:49 PM
RE: Adding keys and values to a dictionary - by deanhystad - Nov-19-2020, 04:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 308 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 867 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,415 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,671 Jan-24-2023, 08:22 AM
Last Post: perfringo
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,748 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,588 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,019 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,021 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,947 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,720 Jan-21-2021, 02:21 PM
Last Post: InputOutput007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020