简体   繁体   中英

How do I create x lists in Python (e.g. with a while-loop)?

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?

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.

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)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM