I have a loop to populate a list, using a loop format.
I'm supposed to create a list, then - using a loop - populate the list with N elements, and set each list element to its index multiplied by 10.
It is also possible that N will be 0. In this case I'm supposed to output an empty list [], and this what I think is tripping me up.
list = [N]
for x in range(N):
innerlist = []
for y in range(N):
innerlist.append(list)
list.append(innerlist)
if N == 0:
list = []
print (list)
I thought that the if(N==0) statement would reset the value of the list but it doesn't. I should output [] but instead I output [0].
>>> N = 10
>>> my_list = [iter * 10 for iter in range(N)]
>>> print(my_list)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> N = 0
>>> my_list = [iter * 10 for iter in range(N)]
>>> print(my_list)
[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With