Skip to content

Latest commit

 

History

History
72 lines (39 loc) · 967 Bytes

Inverting_a_Hash.md

File metadata and controls

72 lines (39 loc) · 967 Bytes

CodeWars Python Solutions


Inverting a Hash

Summary

Given a Hash made up of keys and values, invert the hash by swapping them.

Examples

Given:

  { 'a' : 1,
    'b' : 2,
    'c' : 3 }

Return:

  { 1 : 'a',
    2 : 'b',
    3 : 'c' }



Given:

  { 'foo'   : 'bar',
    'hello' : 'world' }

Return:

  { 'bar'   : 'foo',
    'world' : 'hello' }

Notes

  • Keys and values may be of any type appropriate for use as a key.
  • All hashes provided as input will have unique values, so the inversion is involutive. In other words, do not worry about identical values stored under different keys.

Given Code

def invert_hash(dictionary):
    pass

Solution

def invert_hash(dictionary):
    return {v:k for k,v in dictionary.items()}

See on CodeWars.com