-1

I have a couple lists (raw data from elsewhere), that I collect in another list, to do stuff with later in the code. (So if I were to edit the raw data I am using, I can just change the original lists, edit the everything-list to reflect added/removed lists, and have all the subsequent code reflect those changes without me having to change anything in the rest of the code.)

Like so:

a=[1,2,3]
b=[55,9,18]
c=[15,234,2]

everything=[a,b,c]

At one point I would like to use the NAMES of my original lists ('a','b', and 'c' in my example). Is there a way for me to use my list 'everything' to access the names of the lists put in it?

(So for the code

for i in range(len(everything)):
    print('The data from',???,'is',everything[i]) 

??? would be replaced by something to ideally print

The data from a is [1, 2, 3]
The data from b is [55, 9, 18]
The data from c is [15, 234, 2]

)

id est
  • 1
  • 3
    Possible duplicate of [How to access variable by id?](https://stackoverflow.com/questions/14257350/how-to-access-variable-by-id) – Chris Apr 22 '19 at 06:50
  • "At one point I would like to use the NAMES of my original lists ('a','b', and 'c' in my example). Is there a way for me to use my list 'everything' to access the names of the lists put in it?" In general *no you cannot*. Objects have no knowledge of the variable names that *happen* to be referencing them. You should keep track of this yourself and not rely on hacks involving introspecting the global namespace. – juanpa.arrivillaga Apr 22 '19 at 07:33

3 Answers3

2

You can use dictionaries for this.

a=[1,2,3]
b=[55,9,18]
c=[15,234,2]

everything={'a':a,'b': b,'c': c}

for i in range(len(everything['a'])):
    everything['a'][i] += 10

print(everything)
# >> {'a': [11, 12, 13], 'b': [55, 9, 18], 'c': [15, 234, 2]}

print(a)
# >> [11, 12, 13]

for var, val in everything.items():
    print(f'The data from {var} is {val}') 

"""
>>The data from a is [11, 12, 13]
The data from b is [55, 9, 18]
The data from c is [15, 234, 2]
"""
Shuvojit
  • 1,390
  • 8
  • 16
  • 1
    Instead of the first `for` loop, you can simply do: `everything['a'] = [i + 10 for i in everything['a']]` or `everything['a'] = list(map(lambda x: x+10, everything['a']))` – glhr Apr 22 '19 at 07:00
0

There's a way you can do this, but using a dictionary is equivalent to your case as its keys are unique and can be used as your variable name. Hence, with dictionaries you can retrieve values and print them in any format you need:

a = [1,2,3]
b = [55,9,18]
c = [15,234,2]

everything= {'a': a, 'b': b, 'c': c}

for k, v in everything.items():
    print(f'The data from {k} is {v}')
Austin
  • 25,759
  • 4
  • 25
  • 48
0

If you are trying to access the variable name using id, this can be used.

a=[1,2,3]
b=[55,9,18]
c=[15,234,2]
everything = [a,b,c]

def get_name(your_id):
    name = [x for x,_ in globals().items() if id(_)==your_id ]
    return(name[0])

for i in range(len(everything)):
    print('The data from',get_name(id(everything[i])),'is',everything[i]) 

This outputs:

('The data from', 'a', 'is', [1, 2, 3])
('The data from', 'b', 'is', [55, 9, 18])
('The data from', 'c', 'is', [15, 234, 2])

globals is a built-in which returns a dict of variables/values in the global name space. So you could get the variable name given the id.

void
  • 2,571
  • 2
  • 20
  • 35