Skip to content

Instantly share code, notes, and snippets.

@huttj
Last active August 29, 2015 14:08
Show Gist options
  • Save huttj/220afc4d423d661a55a6 to your computer and use it in GitHub Desktop.
Save huttj/220afc4d423d661a55a6 to your computer and use it in GitHub Desktop.
to_dict: Turn variables into a dictionary
# Turn a list of variables into a dictionary, keyed on the variable names.
# e.g., apple = 2
# to_dict(apple) == {'apple': 2}
def to_dict(*args):
dict = {}
for key in globals().keys():
for var in args:
if (var is not None) and (eval(key) == var):
dict[key] = var
if len(args) == len(dict):
break
return dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment