[Solved] Create a list for each item in another list


I wouldn’t recommend this course of action.

You will probably get all kind of replies telling how you can do this using setattr and friends, but in practice dynamically-created variable names are almost invariably (see what I did there?) a bad idea.

Suppose you already have a variable called X_list and then you process a list containing 'X'. Your variable will have been overwritten.

Further, since you cannot know in advance what variable names will have been created, you will be forced to use dynamic techniques like getattr to access them. This almost guarantees your code will be obscure and difficult to read.

If you REALLY want to access them by name the easiest way to do this is to use the names as keys for a dictionary. This is about the simplest way to do that.

list_dict = {}
for name in ['NSW', 'ACT', 'VIC']:
    list_dict[name+'_list'] = []

My preference would be to drop the '_list' suffix and simply use the names as they appear in the list.

solved Create a list for each item in another list