Reputation: 1006
I want the list of numbers as:
n = int(input("Enter count")
if n=1 , lst =["1"]
if n=2, lst=["1","2"]
if n=3, lst =["1","2","3"]
The list should contain the elements as strings, not integers.
Any idea how to do this?
Upvotes: 1
Views: 775
Reputation: 1
You could use for-loop, and each iteration append i+1
to a list, Since for-loop
start from index 0:
n = int(input("n:")) #5
l = []
for i in range(n):
l.append(str(i+1))
print(l) #['1', '2', '3', '4', '5']
With list comprehension:
print([str(i+1) for i in range(n)])
Upvotes: 0
Reputation: 1
You can use map and range..
n = int(input('Enter count:')
lst = list(map(str, list(range(1,n+1))))
Upvotes: 0
Reputation: 2066
This code is efficient
n = int(input("Enter count:"))
lst = [str(x) for x in range(1,n+1)]
Upvotes: 0
Reputation: 9197
You can use range to create the numbers and since you want strings, you can afterwards map them to strings:
n = int(input("Enter count")
list(map(str, range(1, n+1)))
Upvotes: 1