I was trying to create a method that does a loop and in the process it creates array with 2 levels.
In PHP it would look like this
for($a = 0; $a < 3; $a++) {
$something[$a] = [1,2,3];
}
But the problem is with python. I can’t seem to add the key “u” to the variable and the array under it in the same time. How would you do this properly?
My current code:
for u in range(3):
something[u] = [1,2,3] #this line doesn't seem to work
somethingElse = [1,2,3] #this works properly
Please excuse me if this is a stupid question. I am a beginner and I already tried googling it, but didn’t actually find anything useful.
The best way to find information that you need is to use python tutorial. For example to answer your question yourself you can take a look at the following link: http://docs.python.org/tutorial/datastructures.html
You can use list comprehension:
OR map:
OR insert(if you need to insert into the exact index) or append to add item to the end of list:
If you would like to use mappings(dictionary):