1

I just want to declare some range of lists depending on the input.

For example, if I give the input as 4, I would be expecting 4 lists like

list1 = []
list2 = []
list3 = []
list4 = []

I have tried this

for i in range (1,4):
   list(%d) %i= []

But its not working.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
user3275349
  • 69
  • 1
  • 9
  • 2
    Why do you think you need this? You can just make a list of lists. Or a dict of lists, if you want to give the lists names. – PM 2Ring Apr 21 '16 at 14:13
  • Also see http://stackoverflow.com/questions/14241133/how-can-i-create-lists-from-a-list-of-strings – PM 2Ring Apr 21 '16 at 14:25

1 Answers1

0

I agree with PM-2ring's that in most contexts a list of lists or a dict would be more appropriate.

d = {}
for i in range(1,4):
    d['list%s' % i] = []

or

l = [[] for x in range(1,4)]

But to answer the question actually asked in case this is one of the very rare cases where it is the right thing to do even with all the problems associated with dynamic creation of variables (maintainability, access, sanitizing user input, etc).

You probably shouldn't do any of the below if it is new information to you.

It depends on the context a bit. If you're inserting them into an object it would be:

a = classA()

for i in range(1,4):
    setattr(a, 'list%s' % i, [])

If trying to create a global var:

for i in range(1,4):
    globals()['list%s' % i] = []
nephlm
  • 543
  • 3
  • 11
  • Can someone add a comment on why this is being voted down. I assume it is wrong in some fundamental way I don't see. – nephlm Apr 21 '16 at 14:26
  • 1
    Using `globals` or `setattr` to create dynamic attributes is never a good idea. – Morgan Thrapp Apr 21 '16 at 14:36
  • If you remove that `globals()` example then some of the down-voters may reverse their votes... – PM 2Ring Apr 21 '16 at 14:39
  • 2
    I didn't downvote, but it's not a good idea to show that `globals()` construction to a new Python programmer who asks a question like this. They need to learn the simple lists of lists or dict of lists techniques first. Even the `setattr` stuff is probably too advanced for them at this stage. Often new programmers who see that `globals()` thing then go on to use it in horrible code that becomes the basis of subsequent questions. :) Even worse is when they see solutions that use `exec` or `eval`... – PM 2Ring Apr 21 '16 at 14:40
  • 1
    Also, as Morgan said, even though you _can_ do those things with `globals` or `setattr` it's rarely a good idea since it makes code harder to debug and maintain for little real benefit. And if you're using unsanitized user input to create the names that's a recipe for disaster. Of course, that's not an issue here, since the OP only wants to use numerical suffixes, but it's still not a good mindset to encourage. – PM 2Ring Apr 21 '16 at 14:46
  • Ah. I get that. To eager to answer the actual question. I rearranged it and put in some appropriately dire warnings. – nephlm Apr 21 '16 at 15:04