I have a list of variables in python. And I want to create a dictionary whose key will be the name of the variable and the value will be the content.
a,b,c = 1,2,3
list = [a,b,c]
dic = {f'{item=}'.split('=')[0]:item for item in list}
If I run the previous code, I get as result the following:
{'item': 3}
And i would like to get the following:
{'a':1,'b':2,'c':3}
Thanks in advance
One potential way of doing it is like so (see https://stackoverflow.com/a/592891/11530613):
This gives:
This is definitely a bad idea though, especially with your toy example. For example, integers in Python are the same "object"—if I say:
I get
True, even though I'm testing identity and not equality. So the moment I have another variable with the same value from your toy example above, theistest might evaluate to True, and you could get bad results. Case in point:yields
This is simply an explanation of how you could do it, not that you ever should do it this way. But if you had like, a very short notebook or something and you knew no other variables would ever overlap, you might hack it this way.