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
#1
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))
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 411 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  need to compare 2 values in a nested dictionary jss 2 912 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,481 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,767 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,781 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,651 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,076 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,070 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,964 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,771 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