Skip to main content
Added explanation of the motivation behind this approach
Source Link
Mark Amery
  • 158.3k
  • 93
  • 435
  • 476

To do this while preserving the type of your mapping (assuming that it is a dict or a dict subclass):

def inverse_mapping(f): return f.__class__(map(reversed, f.items())) 
def inverse_mapping(f): return f.__class__(map(reversed, f.items())) 

To do this while preserving the type of your mapping (assuming that it is a dict or a dict subclass):

def inverse_mapping(f): return f.__class__(map(reversed, f.items())) 
Source Link
fs.
  • 928
  • 6
  • 8

def inverse_mapping(f): return f.__class__(map(reversed, f.items()))