Home > Blockchain >  Storing all elements as a list in Python
Storing all elements as a list in Python

Time:07-31

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]

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

Just print the list itself:

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

# [0, 1, 2]

CodePudding user response:

Convert the range to list and print it.

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