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
...
cs95
  • 379,657
  • 97
  • 704
  • 746
Mahdi
  • 967
  • 4
  • 18
  • 34
  • Not sure I understand your objective. Can you please rephrase your question or elaborate on what you are trying to accomplish? – Levon Jun 09 '12 at 19:10
  • Yes what exactly do you mean by "lists with dynamic names"? – Joel Cornett Jun 09 '12 at 19:11
  • 1
    [c for c in 'dynamic names']...sorry...couldn't help it – Gerrat Jun 09 '12 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 – jdi Jun 09 '12 at 19:22

2 Answers2

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.

Community
  • 1
  • 1
weronika
  • 2,561
  • 2
  • 24
  • 30
  • This will do if you are interested :-) `"list{} = []".format(i)` [I'm just transitioning to `.format` from the old-style myself] – Levon Jun 09 '12 at 19:25
  • 1
    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 Jun 09 '12 at 19:25
  • 1
    @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. – weronika Jun 09 '12 at 19:27
  • 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" ;) – jdi Jun 09 '12 at 19:30
  • What is funny is that if you google "why creating dynamic variables is bad" almost every summary snippet in the results contain "it is bad practice", for multiple languages – jdi Jun 09 '12 at 19:33
  • @jdi - hopefully the edited version contains enough reinforcement? Feel free to point me to better links explaining why it's bad. – weronika Jun 09 '12 at 19:36
  • @Levon - thanks! Added the new-style version to the answer. I really should learn that someday... – weronika Jun 09 '12 at 19:37
  • Instead of that nasty `exec` I'd rather use `locals()['list%d' % i] = []` - but of course not creating dynamic variable names is much better! – ThiefMaster Jun 09 '12 at 19:40
  • I removed the craziness about making dynamic variable names. It's just silly, and an attractive nuisance. Better to tell people the good way to do things. – Ned Batchelder Jun 09 '12 at 20:11
  • @NedBatchelder - oops, I was editing at the same time you were and your edit got overwritten... I *did* give the good way to do things first, but I think more infomation is better than less (again, consenting adults etc). But go ahead and redo your edit if you think it's that bad. – weronika Jun 09 '12 at 20:18
  • With due respect to @ThiefMaster, changing `locals` in this way is specifically warned against in the [documentation](http://docs.python.org/library/functions.html#locals) for `locals()`: "Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter." – DSM Jun 09 '12 at 20:18
  • @ThiefMaster - excellent point, I totally forgot about that. Changed. Thanks! – weronika Jun 09 '12 at 20:18
  • @DSM - Huh, I didn't know that... Yeah, dynamic variables really *are* a terrible idea either way. I ended up just putting a long note on why dynamic variables are bad, along with a short reference to `exec` and `locals` for completeness of information, with a link on why each is a bad idea. Hopefully that's a reasonable solution. – weronika Jun 09 '12 at 20:26
  • 1
    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. – jfs Jun 09 '12 at 20:44
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.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99