3

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

2
  • please fix your indentation Commented Feb 3, 2018 at 18:07
  • 2
    please give an idea of an input and output. also, never call a variable by a class name, e.g. use lst for a variable name, not list. Commented Feb 3, 2018 at 18:25

5 Answers 5

7
>>> 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)
[]
Sign up to request clarification or add additional context in comments.

Comments

1
lst = range(8)
innerlst = []


for i in lst:
    if 0 in lst:
       innerlst = []
       continue
    else:
       innerlst.append(i*10)

Comments

0

If you just add the condition before entering the whole loop, you'll get the expected result:

N = 10
list = []
if (N > 0):
    for i in range(N):
        list.append(i*10)
print(list)
# output: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Comments

0
myList = []

if N == 0:
  print(myList)
else:  
  for i in range(N):
    myList.append(i * 10)
  print(myList)

2 Comments

Welcome to SO! I'm coming from the reviews queue because this was flagged as low-quality. A little explanation also goes a long way with answers. Code-only answers are often outright deleted because they don't explain how they work (which is what will really help OP and incoming readers).. additionally/unfortunately this code snippet also doesn't work.
Assuming the OP is also using the same Codio environment, this snippet does in fact work. I will add comments in the future, thanks.
-1

I'm just a beginner myself, but I found this online that matches your question. I have no idea how to format the actual code right, but hopefully this helps!

my_list = []
my_newlist =[]
if N <= 0:
    print(my_list)
else:
    for x in range(N):   
        my_list.append(x)   
        my_newlist= [i* 10 for i in my_list] # not getting the statement 'i * 10 for in i' statement
    print(my_newlist)

[Python] #We provide you with a number N. #Create a list then, using a loop, populate - Pastebin.com. (2017, November 29). Retrieved from https://pastebin.com/2F0zXq6z

1 Comment

There is a {} button that formats the code that you selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.