What is the best way to create lists dynamically in Python?

My users will be creating an arbitrary number of Human objects and grouping them in as many groups as they like. (The number of groups created will be tracked) I want to create a list for each group that they've created to use later. How can I create a list for each group if I don't know how many groups there will be?

I've thought of something like the following:

for i in range(num_groups):
    eval('group_' + str(i) + ' = []')

which should create an empty list for group i named group_i. I've read that eval is not something that should be used in most cases, so I'm wondering if there's a better way.



Read more here: https://stackoverflow.com/questions/66482268/what-is-the-best-way-to-create-lists-dynamically-in-python

Content Attribution

This content was originally published by JerAnsel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: