Created
October 24, 2018 09:23
-
-
Save devhero/360157e1200a6213306458e714e8d6a2 to your computer and use it in GitHub Desktop.
recursive operation on key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lower(obj): | |
return obj.lower() | |
def lower_dict(obj): | |
return _recursive_key_op(obj, lower) | |
def _recursive_key_op(obj, op=None): | |
if isinstance(obj, dict): | |
return {op(k): _recursive_key_op(v, op) for k, v in obj.items()} | |
elif isinstance(obj, (list, set, tuple)): | |
t = type(obj) | |
return t(_recursive_key_op(o, op) for o in obj) | |
elif isinstance(obj, str): | |
return op(obj) | |
else: | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment