Hello I was wondering how I would print the variable names inside of my list. Thanks
traits = [Character, Eye, Hair, Mouth, Shirt]
attributes.append({'trait_type': f"{traits=}", 'value': (os.path.basename(traits[a]))[:-4]})
What I want the output to be is:
'trait_type': 'Character'
I was searching and alot of threads said to make a dictionary, but wasn't sure if there was a way to brute force it. Thanks.
CodePudding user response:
Replace the code to be the following
mydict = {
'var1-name': 'var1-value',
'var2-name': 'var2-value',
'var3-name': 'var3-value'
}
for name in mydict:
print(name, mydict[name])
Why ?
To be honest, because you cannot print an variable name inside a list,
you can check here --> How to print variable name in a list
Solution
We need to change the list to become a dictionary to be able to print variable name in Python.
CodePudding user response:
one way of doing this:
first = 'green'
second = 'yellow'
third = 'orange'
mycolors = ['first','second','third']
for each in mycolors:
print(each, vars()[each])
Output
first green
second yellow
third orange
