Last active
August 29, 2015 14:08
-
-
Save huttj/220afc4d423d661a55a6 to your computer and use it in GitHub Desktop.
to_dict: Turn variables into a dictionary
This file contains hidden or 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
# 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