Member Avatar for Mouche

I'd like to flip the keys and the values in a dictionary.

Say I have this dictionary:

positions = {'secretary' : 'Jessica', 'IT manager' : 'Matt', 'sales worker' : 'Linda', 'boss' : 'Chris'}

and I want this:

employees = {'Jessica' : 'secretary', 'Matt' : 'IT manager', 'Linda' : 'sales worker', 'Chris' : 'boss'}

Is there an easy way to create "employees" such as employees = positions.function() Thanks for the help.

Make sure your dictionary values are unique and immutable when you do this:

inverted_dict1 = dict([(v, k) for (k, v) in dict1.iteritems()])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.