python-benedict/benedict/core/invert.py

13 lines
281 B
Python
Raw Normal View History

# -*- coding: utf-8 -*-
def invert(d, flat=False):
new_dict = d.copy()
new_dict.clear()
for key, value in d.items():
if flat:
new_dict.setdefault(value, key)
else:
new_dict.setdefault(value, []).append(key)
return new_dict