0

Consider the list of variables and a Python list:

apple='green'
banana='yellow'
orange='orange'

fruits = [apple, banana, orange]

How to print the names of variables, instead of its values when lopping over the elements of the list?

for fruit in fruits:
    #print the names of variables, that is "apple", "banana", etc.

I know that a dictionary would be a better fit in this problem but for the sake of argument, is it possible to do it with a list?

cerebrou
  • 5,353
  • 15
  • 48
  • 80
  • 2
    Possible, if you can guarantee that no other variables have the same value. Not something you'd want to rely on, though. See [`globals()`](https://docs.python.org/3/library/functions.html#globals). – glibdud May 28 '19 at 14:36
  • At first glance, you could probably do something with either `vars()` or `globals()`. However, you’d have to loop over and see which variable names have the value you’re checking. This will be a problem if 2 variables have the same value. As you describe, a `dict` is the best solution here. – N Chauhan May 28 '19 at 14:36
  • 5
    The list doesn't hold the variables; it just holds their values. Use a dict instead. – khelwood May 28 '19 at 14:37
  • A dictionary would definitely be better. See [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4518341) – wjandrea Jul 02 '20 at 01:32

3 Answers3

1

You want this:

apple = "green"
banana = "yellow"
orange = "orange"

fruits = [apple, banana, orange]
for fruit in fruits:
    fruit_name = [key for key, value in locals().items() if value == fruit]
    print(fruit_name[0])
dallonsi
  • 1,299
  • 1
  • 8
  • 29
0

It is a bit of a cheat, but if you store the names of the variables in the fruits array instead of the variable itself, it is trivial:

>>> apple = 'green'
>>> banana = 'yellow'
>>> orange = 'orange'
>>> fruits = ['apple','banana','orange']
>>> for fruit in fruits:
...     print fruit, vars()[fruit]
...     
apple green
banana yellow
orange orange

If you are really wanting to get the variable name, you have a problem as the array is not storing the variable, but the string it is pointing at - for example, you can change the content of apple after the assignment to demonstrate the array holds the original string:

>>> fruity = [ apple, orange, banana ]
>>> apple = 'yikes'
>>> print fruity
['green', 'orange', 'yellow']

The best you can do is build a reverse lookup of the vars() dictionary and attempt to look up the variable...

>>> yy = dict([v,k] for k,v in vars().items() if isinstance(v,str))
>>> for fruit in fruity:
...     print yy.get(fruit,'Unknown'), fruit
...     
Unknown green
orange orange
banana yellow
F1Rumors
  • 920
  • 9
  • 13
-1

use NamedObject, that should work:

apple='green'
banana='yellow'
orange='orange'
fruits = [apple, banana, orange]    

apple = NamedObject('apple', apple)
print(apple)
print(apple.name)

this should print:

green
apple

If you dont understand, here is another similar thing: Access the name of a list in a Python print statement

wondercoll
  • 339
  • 1
  • 4
  • 15