0

How can I create lists with dynamic names in python, for example

for i in range(len(myself)):
   list(i) = []

what should I use instead of list(i) ? it means that i want some names as below:

list1
list2
list3
...
4
  • Not sure I understand your objective. Can you please rephrase your question or elaborate on what you are trying to accomplish? Commented Jun 9, 2012 at 19:10
  • Yes what exactly do you mean by "lists with dynamic names"? Commented Jun 9, 2012 at 19:11
  • 1
    [c for c in 'dynamic names']...sorry...couldn't help it Commented Jun 9, 2012 at 19:12
  • 8
    I have seen this question more than once on here and it usually ends with there being a better way to solve your problem... Like a dict Commented Jun 9, 2012 at 19:22

2 Answers 2

7

I'd advise you to just use a list or dictionary instead of dynamic variable names. All the versions below result in lists[0], lists[1] etc being [], which seems close enough to what you want, and will be more readable/maintainable in the long term. (Note: I'm using lists instead of list as a variable name because the latter would overwrite the builtin list function, which you probably don't want).

1) Version with lists being a list of lists (the numbers are just the order of the lists):

lists = [[] for i in range(len(myself))]

2) Same but with a for loop instead of a list comprehension:

lists = []
for i in range(len(myself)):
   lists.append([])

3) Version with lists being a dictionary of lists with numbers as keys (a bit more flexible if you want to remove some of the values later or such):

lists = {}
for i in range(len(myself)):
   lists[i] = []

About dynamic variable names, i.e. variables like list1 instead of lists[1]... Seriously, you probably shouldn't do that. It's unnecessarily complicated and hard to maintain. Think about it - next month you'll want to modify the script, and you'll try to figure out where the variable list1 was defined, and you won't be able to do that with a plain text search. It's a pain.
But if you really want to for some reason, it's possible with exec - here are some reasons not to use it - or with modifying locals() - bad idea according to documentation. Also see comments for more discussion on why these things are a bad idea and how confusing it gets even talking about them.

Sign up to request clarification or add additional context in comments.

14 Comments

This will do if you are interested :-) "list{} = []".format(i) [I'm just transitioning to .format from the old-style myself]
I would seriously consider removing that part about the dynamic variable with exec. Its a terrible idea and shouldnt even be made available as an option.
@jdi - it's a bad option, but consensual adults and all... I'm currently searching SO for good arguments against using it and will add links, if that helps.
Yea you should make sure to reinforce why its a terrible practice. Its kinda like saying "you shouldnt murder someone, but just in case you are curious, you could kill someone pretty easily using these outlined methods" ;)
btw, an object refered as lists[0] already has a name: it is 'lists[0]' itself. In a way it is literally a dynamic variable name.
|
5

Dynamic variable names are very rarely a good idea; it is almost always better to use a dictionary:

myLists = {"list{}".format(i):[] for i in range(len_i)}

Do not use list as a variable name - it hides the existing keyword.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.