4

I am running Python 3.6 and Pandas 0.19.2 and want to access the name of a list as follows:

# work on the following two lists
for list in [list1, list2]: 
    # loop through items in each list
    for entry in bucket_set: 
        # do stuff
    # let user know I finished working on list x
    print('i just finished working on: '+list)

In the first iteration, this should do stuff and then print out: 'I just finished working on list1'. However: when I run my code, I get the following error:

TypeError: must be str, not list

This makes perfect sense (a list is not a string after all), but is there any way I can get the name of my list as a string ('list1')? I know I can use a dict instead (which is what I'm doing right now), but I'm interested nonetheless.

Martin Reindl
  • 989
  • 2
  • 15
  • 33
  • 4
    No, you _should_ use a dict. Variables are just like sticky note tags for objects in Python, and there isn't much reason to be trying to produce them as strings. Is it possible? By looking into `locals()`, maybe to some extent. – miradulo Feb 02 '17 at 16:45
  • 2
    No. The name `list` (which you shouldn't use, there's a function named `list`), is mapped to the same value as the name `list1` in the first iteration of your loop, but there is no relation between `list` and `list1` – Patrick Haugh Feb 02 '17 at 16:45
  • 1
    Not easily or reliably, the object has no knowledge of its name(s) and could have many different names in various scopes. You could do something weird like search through `globals()` or `locals()` comparing each value to your object using `is`, but this is quite the work-around that may fail in many situations. – Jared Goguen Feb 02 '17 at 16:45
  • 2
    Names refer to objects, but objects know nothing about what names are bound to them; read http://nedbatchelder.com/text/names.html – chepner Feb 02 '17 at 16:45
  • 1
    This question looks similar to this one :- [Python Introspection - How can i get name of the object?](http://stackoverflow.com/questions/1538342/how-can-i-get-the-name-of-an-object-in-python) – Kiran Bhagwat Feb 02 '17 at 16:51
  • Guess I'll just stick with dictionaries then. Thanks for all the answers! I learned something today :-)... – Martin Reindl Feb 02 '17 at 16:56

3 Answers3

2

You could create a class to generally encapsulate a name, but this is a lot of overhead when a dictionary would suffice.

class NamedObject:
    def __init__(self, name, obj):
        self.name = name
        self.obj = obj

    def __getattr__(self, attr):
        if attr == 'name':
            return self.name
        else:
            return getattr(self.obj, attr)


unnamed_list = [1, 2, 3]
named_list = NamedObject('named_list', unnamed_list)

print(named_list) # [1, 2, 3]
print(named_list.name) # 'named_list'
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
1

You can create class or dictionary, or you can iterate your variables.

my_list = [1,2]
my_var_name = [k for k,v in locals().items() if v == my_list][0]
print(my_var_name)

It gives 'my_list'.

Beware that lists values need to be unique!

Daniel Malachov
  • 1,604
  • 1
  • 10
  • 13
-5

There are many options, one of them is repr(list)

Humberto Pinheiro
  • 1,082
  • 1
  • 13
  • 19