-1

I am trying to generate callable lists that looks like this: Alist = [A1, A2, A3,..., An] Blist = [B1, B2, B3,..., Bn] Clist = [C1, C2, C3,..., Cn] ...

I tried:

N = 5
Lists = ["A","B","C","D"]
for j in Lists:
    def jList(N):
        lst = []
        for i in range(n+1):
            lst.append(j+str(i))
        return(lst)

and this generates the correct lists, but it doeas not create the correct and callable name of the lists. So it gives:[A1, A2, A3, A4, A5], but not AList = [A1, A2, A3, A4, A5]

As all the Names in the List are defined before (Let´s say A1 = 1, A2 = 2, A3 = 3, A4 = 4, A5 = 5), i need to be able to call this numbers. So i need to call A[3] and get "3" in return for making the code lines variable, which i hard coded so far.

I also tried an approach like:

N = 5
Lists = ["A","B","C","D"]
def createList(j,n):
    lst = []
    for i in range(n+1):
        lst.append(j+str(i))
    return(lst)
for j in Lists:
    j+"List" = createList(j,N)

but this does not work.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
LeBron10
  • 3
  • 2
  • 1
    You're using the term "call" incorrectly. What exactly do you mean? Are you saying you want to assign them to variables? If so, then you have the wrong goal. Instead, keep the values in a dictionary, using the names as the keys. – Tom Karzes Apr 05 '23 at 09:28
  • If I understand what you're looking for, see: https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables – slothrop Apr 05 '23 at 09:41

2 Answers2

1

I think what your are looking for is a dict:

n = 5
lists = ["A","B","C","D"]
list_dict = dict()
for list_name in lists:
    list_dict[list_name] = [list_name + str(i) for i in range(1,n+1)]

Now you can acces your lists like this: list_dict[list_name][index], for example list_dict["C"][3] would give "C3". Is this what your are looking for?

lotus
  • 116
  • 6
-1

I believe what you are looking for is here Python program to create dynamically named variables from user input

So from your example, the following snippet should get close to what you are trying to achieve.

# This creates AList, Blist, etc. 
N = 5
Lists = ["A","B","C","D"]
def createList(j,n):
    lst = []
    for i in range(n+1):
        lst.append(j+str(i))
    return(lst)
for j in Lists:
    globals()[f"{j}List"] = createList(j, N)

# To test it, I assume A0 is hardcoded. 
A0 = 5 
Index = 0 

print (globals()["AList"]) # Output: ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']

print (eval(globals()["AList"][Index])) # Output: 5 

Zaid Afaneh
  • 114
  • 1
  • 10
  • Thank you very much. This solves some of my errors, but with this i´m not able to assign values to variable names. I can not code eval(globals()["AList"][2]) = 5 for example, but i need a variable assignment like: A[n+1] = 5 – LeBron10 Apr 05 '23 at 11:13
  • 1
    @LeBron10 you should *absolutely not do this*. Do **not** dynamically create variables. – juanpa.arrivillaga Apr 05 '23 at 18:02
  • @juanpa.arrivillaga I am sure creating variables dynamically is wrong, but why ? I am not really experiences with this part and would appreciate more details here. Thank you in advanced. – Zaid Afaneh Apr 05 '23 at 22:00