0

I want to update some lists they are named like

ACL = []
ANL = []
APL = []
# more around 50+ 

I also have these names in the form of str inside a tuple

CHOICES = [ 'ACL', 'ANL', 'APL' ]

Now I'm making a query into the database when I'm getting the results of ACL I want to update the ACL list. There's no problem in querying data and appending it to the list. But my problem is I have to query and append each list independently. But I want to utilize the choices, iterating them and updating the relative list

That's my current approach:

acl_filtered_data = [{"results" : "result"} for j in qs if j.choice == 'ANL']
ACL.append(acl_filtered_data)

And the same for the other choices.

So here I want to run a loop for all the available choices and append it into the related list.

for i in CHOICES:
    filtered_data = [{"results" : "result"} for j in qs if j.choice == i]
    # here I want to point ACL = [] and append it with the filtered_data and also keep repeating for the others 
    # I'm not sure how I can do that.

I need your help to achieve this kind of logic. Thanks

atropa belladona
  • 474
  • 6
  • 25
  • what is qs? and what is your desired output or end result? – Alexander Aug 16 '22 at 06:07
  • This dosnt matter here , Whatever the qs or results is but I want to append filtered_data to the ACL = [] ANL = [] APL = [] – atropa belladona Aug 16 '22 at 06:09
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Chris Aug 16 '22 at 06:31

3 Answers3

2

Create a dictionary or list of tuples, like you had before, where each string references the corresponding list.

Then on each iteration you have a reference to the list that you need to append to.

ACL = []
ANL = []
APL = []
CHOICES = {'ACL': ACL, 'ANL': ANL, 'APL': APL}
  
...

for key, value in CHOICES.items():
    filtered_data = [{"results" : "result"} for j in qs if j.choice == key]
    value.append(filtered_data)

Or using tuples.

ACL = []
ANL = []
APL = []
CHOICES = [('ACL', ACL), ('ANL', ANL), ('APL', APL)]
  
...

for text, lst in CHOICES:
    filtered_data = [{"results" : "result"} for j in qs if j.choice == text]
    lst.append(filtered_data)
Alexander
  • 16,091
  • 5
  • 13
  • 29
1

For this you can use eval() function. You can pass any Python statement into the eval() function as a string and it will be executed. See the below example(got it from the official documentation),

>>> x = 1
>>> eval('x+1')
2

With this example, you can use your own code by changing a singe line of code.

ACL = []
ANL = []
APL = []

CHOICES = [ 'ACL', 'ANL', 'APL' ]

for i in CHOICES:
    filtered_data = [{"results" : "result"} for j in qs if j.choice == i]
    eval(f'{i}.append({filtered_data})')

This will dynamically update your list. The statement eval(f'{i}.append({filtered_data})') is similar to,

ACL.append(filtered_data)
ANL.append(filtered_data)
APL.append(filtered_data)

Refer to this Python eval() article to get some hands-on experience.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
1

It think what you're looking for is vars([object]):

ACL = []
ANL = []
APL = []

# E.g. get a reference to ACL using vars():
x = vars()['ACL']

# x is now a reference to ACL
# As lists are mutable, any operation on x will directly change ACL
print(x is ACL)
x.append('test')
print(ACL)

# Now we switch to ANL for example
x = vars()['ANL']

# Now any operation on x changes ANL
print(x is ANL)
x.append('test2')
print(ANL)
print(ACL)

A simpler and more intuitive approach would be to store your data in a dictionary:

# Store data in a dictionary
data = {
    'ACL': [],
    'ANL': [],
    'APL': [],
    }

# The keys of the dictionary are the valid choices
CHOICES = list(data.keys())

# Now you can simply index your data by the name of the according list:
data['ACL'].append('test')
print(data['ACL'])
slarag
  • 153
  • 8