Storing all elements as a list in Python

Question:

I want to print all elements as a list. I have included the current and expected outputs.

for i in range(0,3):
    P=i
print(P)

The current output is

0
1
2

The expected output is

[0,1,2]
Asked By: user19657580

||

Answers:

Just print the list itself:

print(list(range(0, 3)))

# [0, 1, 2]
Answered By: 101
lst = [i for i in range(0, 3)]
print(lst)
Answered By: NickName11

Convert the range to list and print it.

elems = list(range(0,3))
print(elems)
Answered By: Shuhana Azmin
l = list() # create an empty list
for i in range(0,3):
    l.append(i) # append the list with current iterating index
print(type(l)) # <class'list'>
print(l)
Answered By: ARYAN KUMAWAT
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.