Skip to content

Instantly share code, notes, and snippets.

@devhero
Created October 24, 2018 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devhero/360157e1200a6213306458e714e8d6a2 to your computer and use it in GitHub Desktop.
Save devhero/360157e1200a6213306458e714e8d6a2 to your computer and use it in GitHub Desktop.
recursive operation on key
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