Reputation: 1231
I have an array which created with this:
numbers = []
for num in range(0,5):
nums = []
for inum in range(0,5):
nums.append(inum)
numbers.append(nums)
How to loop through? I tried for item in len(numbers):
but it doesn't work.
Upvotes: 0
Views: 12746
Reputation: 1886
Try to this.
[[col for col in range(5)] for row in range(5)]
Upvotes: 0
Reputation: 2953
List items are iterable, no need to get length as they will assign themselves to the first variable with for..in.. loops;
for item in numbers:
print "In first list: ", item
for num in item:
print " Getting number: ", num
outputs
In first list: [0, 1, 2, 3, 4]
Getting number: 0
Getting number: 1
Getting number: 2
Getting number: 3
Getting number: 4
In first list: [0, 1, 2, 3, 4]
Getting number: 0
Getting number: 1
...
Upvotes: 4
Reputation: 40688
Double loop:
for row in numbers:
for cell in row:
print cell,
print
Upvotes: 1