Menu

gitpiper

invert_dictionary python Code Snippet in 2023

dictionaryintermediate

Last Updated: 2 February 2023

Inverts a dictionary with unique hashable values.

  • Use dictionary.items() in combination with a list comprehension to create a new dictionary with the values and keys inverted.
def invert_dictionary(obj): return { value: key for key, value in obj.items() }
ages = { 'Peter': 10, 'Isabel': 11, 'Anna': 9, } invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' }

python snippet similar to invert_dictionary For You in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️