8

I am a beginner in python and met with a requirement to declare/create some lists dynamically for in python script. I need something like to create 4 list objects like depth_1,depth_2,depth_3,depth_4 on giving an input of 4.Like

for (i = 1; i <= depth; i++)
{
    ArrayList depth_i = new ArrayList();  //or as depth_i=[] in python
}

so that it should dynamically create lists.Can you please provide me a solution to this?

Thanking You in anticipation

falsetru
  • 314,667
  • 49
  • 610
  • 551
Cheese
  • 225
  • 2
  • 3
  • 9

3 Answers3

12

You can do what you want using globals() or locals().

>>> g = globals()
>>> for i in range(1, 5):
...     g['depth_{0}'.format(i)] = []
... 
>>> depth_1
[]
>>> depth_2
[]
>>> depth_3
[]
>>> depth_4
[]
>>> depth_5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'depth_5' is not defined

Why don't you use list of list?

>>> depths = [[] for i in range(4)]
>>> depths
[[], [], [], []]
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • Thanks..But Iam getting an error while using the first method g['depth_{}'.format(i)] = [] ValueError: zero length field name in format – Cheese Aug 07 '13 at 09:13
  • @ChithraNair, I updated the code to run on Python 2.6. – falsetru Aug 07 '13 at 09:13
  • while trying to print depths_1 its telling undefined variable! Yes I think I can go with using list of lists – Cheese Aug 07 '13 at 09:30
  • @ChithraNair, I used `depth_..` instead of `depths_..` for variable names. – falsetru Aug 07 '13 at 09:31
  • Thanks...yes, even i tried with depth_1, it was complaining in red mark of an undefined variable but running it gave the result!!!Thank u so much...By the way, is it a bad choice to use globals here? – Cheese Aug 07 '13 at 09:36
  • 1
    red mark? You maybe using an IDE. Many IDEs does not handle variables created this way. As other answered, making variables this way (using globals) is not good way to go. – falsetru Aug 07 '13 at 09:38
  • Actually i made use of the second method. I appended another list to say depths[1] using depths[1].append(list), before calling this list and depths[1] had values, but after calling append, depth[1] becomes None! Am I doing something wrong?Request you to kindly give some guidelines – Cheese Aug 07 '13 at 12:28
  • @ChithraNair, I can't understand what you comment. Please post another question with the code. – falsetru Aug 07 '13 at 13:05
6

You can not achieve this in Python. The way recommended is to use a list to store the four list you want:

>>> depth = [[]]*4
>>> depth
[[], [], [], []]

Or use tricks like globals and locals. But don't do that. This is not a good choice:

>>> for i in range(4):
...     globals()['depth_{}'.format(i)] = []
>>> depth_1
[]
zhangyangyu
  • 8,052
  • 2
  • 30
  • 42
5

I feel that depth_i is risky and so wouldn't use it. I'd recommend that you use the following approach instead:

depth = [[]]

for i in range(4):
    depth.append([])

Now you can just call depth_1 by using depth[1] instead. If possible, you should start from depth[0].

Then your code will be depth = [] instead.