-1

I would like to create lists in Python but not create each one individually by hand. Instead, I want to create X lists. So the number of lists depends on X.

Is that possible for example with a while-loop? And of course, every list should have a different name.

For example: List_1, List_2, List_3 ... List_x

Does anyone know how to do this?

David
  • 8,113
  • 2
  • 17
  • 36

3 Answers3

0

Why not just create a list/dict that contain the amount of lists desired?

something like:

[[] for _ in range(3)]

Or:

{x:[] for x in range(3)}

Then you can access each "list_i" that you desire by indexing/key.

David
  • 8,113
  • 2
  • 17
  • 36
  • Thank you for your answer. Unfortunately, I'm not that advanced yet. So if I understood that correctly, 3 different lists were generated, right? And what are the names of the individual lists? Thanks again –  Oct 09 '20 at 08:40
  • @IlovePython in the first option there is no "name" to the list. In the second option you could refer the key values as names (but they arent). you create **1** variable with a certain name with multiple lists and then just accessing each list – David Oct 09 '20 at 08:42
0

In my opinion, the only option is to make a list and then append other lists to that list. For example, the code could look like this:

Big_List=[]

x = 0
while x < 100:
    Big_List.append([])
    x = x+1

Then such a list is created in which you can selectively navigate to the individual lists:

Big_List=[[],[],[],[],[],[]]

You can then append something to the 10th list, for example:

Big_List[9].append(item)
Niki
  • 66
  • 1
  • 15
0

Either you create them by hand or you virtually create a list from lists. I think this is the easiest way. Unfortunately you cannot name the lists in the lists. Actually there is no other way. Variable names in variables just do not work!