-1

I am creating a code where I ask users to give certain information. Based on the number of input items, I want to create empty lists that will be used for a for-loop later on.

For instance,

user_info=[19,20,21,22,23]

Desired output:

19=[], 20=[], 21=[], 22=[], 23=[] 

Since the first letter couldn't be number, I wouldn't care if the list name is alphabets.

a=[],b=[],c=[],d=[],e=[]

Thanks!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user7852656
  • 675
  • 4
  • 15
  • 26
  • 3
    You need to be a *lot* more specific here. This sounds like [an XY problem](https://meta.stackexchange.com/q/66377/322040) at best; dynamically creating a bunch of named variables is a terrible idea. It's conceivable making a `list` of `list`s or a `dict` of `list`s might make sense, but not named variables. – ShadowRanger Oct 15 '19 at 00:21
  • As a compromise, as @ShadowRanger mentioned, the dict with values as empty lists would be a good first approach : `{info: [] for info in user_info}` – metasomite Oct 15 '19 at 00:27
  • I suspect this qualifies as a duplicate of [Creating new variables in loop, with names from list, in Python](https://stackoverflow.com/q/11319909/364696). – ShadowRanger Oct 16 '19 at 01:16

2 Answers2

0

Not sure why you would want to do this, but you can use a dictionary:

vars = {}

for i in range(num_inputs):
    vars[i+1] = []

To access your variables, you'd use the dictionary:

vars[23] += [42, 34, 92]
Alec
  • 8,529
  • 8
  • 37
  • 63
0

If I'm understanding it well, u can simply make a dictionary for every input. As long as you don't enter equal numbers, it will work.

For example, if you have the list:

user_info=[19,20,21,22,23]

You can just create a dictionary that would be like :

myDictionary = {19:[],20:[],21:[],22:[],23:[]}

And you can acces to each array just iterating through your user_info array:

for users in user_info:
    print(myDictionary.get(users))

[],[],[],[],[]
Yeste
  • 135
  • 10