Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating lists in python

Tags:

python

list

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].

like image 756
r.hart Avatar asked Jan 18 '26 13:01

r.hart


1 Answers

>>> 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)
[]
like image 184
Patrick Atwater Avatar answered Jan 20 '26 03:01

Patrick Atwater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!