Skip to content Skip to sidebar Skip to footer

Python Declare Multiple Lists

I have more than 300 variables in a list and i have to make a list of each variables: example: x=['aze','qsd','frz']... i want: MAXaze=[] MAXqsd=[] MAXfrz=[] ... without typing t

Solution 1:

You can use a dictionary to store the values:

x=['aze','qsd','frz',...]
vars = {}
for i in x:
    vars["MAX" + i] = []

Or in order for them to become real variables you can add them to globals:

x=['aze','qsd','frz',...]
for i in x:
    globals()["MAX" + i] = []

You can now use:

MAXaze = [1,2,3]

Solution 2:

The easiest way that comes to my mind is by using exec. But be aware that there are security issues related to exec.

x=['aze','qsd','frz']
for i in x:
    exec('{}=[]'.format(i))

Post a Comment for "Python Declare Multiple Lists"