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
You can not achieve this in Python. The way recommended is to use a list to store the four list you want:
Or use tricks like
globals
andlocals
. But don't do that. This is not a good choice:You can do what you want using
globals()
orlocals()
.Why don't you use list of list?
I feel that
depth_i
is risky and so wouldn't use it. I'd recommend that you use the following approach instead:Now you can just call
depth_1
by usingdepth[1]
instead. If possible, you should start fromdepth[0]
.Then your code will be
depth = []
instead.