-1

If I take a take a simple for loop as an example:

for num in range(0,6)
print(num)

it gives me:

0
1
2
3
4
5

How can I get the output as a list, like this:

[0,1,2,3,4,5]

If I do this:

lst = []
for num in range(0,6):
    lst.append(num)
    print(lst)

I get:

[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]

Six lists, but I only want the last one.

Andre Human
  • 25
  • 1
  • 2
  • 8
    Just put the `print` *after* your `for` loop instead of inside it. – Anthony Labarre Jan 22 '19 at 10:08
  • why are you printing list in each iteration? – abhilash_goyal Jan 22 '19 at 10:16
  • 1
    *"Six lists, but I only want the last one."* No... You have one list, you just print it six times – Nick is tired Jan 22 '19 at 10:17
  • Please review the role of indentation in Python, and carefully proofread your code before posting a new question. – TigerhawkT3 Jan 22 '19 at 10:21
  • This is not actually a question about how to make the list. It is a question about understanding what `print` does, and perhaps what `return` does (if the code is inside a function). `lst` **already** contains the value you want; using `print` is **completely unrelated** to that. – Karl Knechtel Jul 01 '22 at 01:10

6 Answers6

1

Just do list to make it a list:

print(list(range(6)))

Output:

[0, 1, 2, 3, 4, 5]

Your code doesn't work because:

  • Because you print every time the loop is going again, so actually your code works if you make print(lst) outside of the loop

  • Your final list is actually your expected list, I'm telling you.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

One way to do using list-comprehension

print([i for i in range(6)])

What's wrong with your code

  • First you are trying to print individual elements where you want output as list
  • In 2nd attempt you just misplaced the print statement inside the loop while it should be outside the for loop.
Sociopath
  • 13,068
  • 19
  • 47
  • 75
0

after completing your for loop then use print

lst = []
for num in range(0,6):
    lst.append(num)
print(lst)
0

this is your current code

lst = []
for num in range(0,6):
    lst.append(num)
    print(lst)

As you can see your print(lst) statement is indented inside the loop so it prints lst every num in range(0,6). To fix this, move it outside the loop so you only print the outcome/last one!

lst = []
for num in range(0,6):
    lst.append(num)
print(lst) # Un-indent :D

Happy Coding, good luck

Gareth Ma
  • 707
  • 4
  • 10
0

If you want to put a for output to the list, your code was correct. You only put "print" in the wrong place.

Just replace this:

lst = []
for num in range(0,6):
    lst.append(num)
    print(lst)

to this:

lst = []
for num in range(0,6):
    lst.append(num)
print(lst)

But there are more effective ways to make a list from numbers range, it was displayed in other comments.

0

The print inside for loop in your code is called for every value of num, that means you begin the for loop, you add num to lstand you print lst. The solution is print lst after adding all value of num to lst, you can put print outside of for

lst = []
for num in range(0,6):
    lst.append(num)
print(lst)
Noureddine
  • 103
  • 1
  • 4
  • 16