user19657580
user19657580

Reputation: 39

Storing all elements as a list in Python

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]

Upvotes: 0

Views: 42

Answers (4)

NickName11
NickName11

Reputation: 11

lst = [i for i in range(0, 3)]
print(lst)

Upvotes: 1

ARYAN KUMAWAT
ARYAN KUMAWAT

Reputation: 36

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)

Upvotes: 2

Shuhana Azmin
Shuhana Azmin

Reputation: 81

Convert the range to list and print it.

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

Upvotes: 0

101
101

Reputation: 8999

Just print the list itself:

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

# [0, 1, 2]

Upvotes: 0

Related Questions