Try this:
lst = input('insert numbers: ')
lst = [int(d) for d in lst]
lst
As your comment, try only this one line:
[int(d) for d in input('insert numbers: ')]
Answer from Mahdi F. on Stack OverflowTry this:
lst = input('insert numbers: ')
lst = [int(d) for d in lst]
lst
As your comment, try only this one line:
[int(d) for d in input('insert numbers: ')]
I believe you can't perform this operation directly without passing to string type because int type is not iterable. In that case, you can just use input() without int().
You can try this 2 situations to create a list:
In this case, numbers without separation would be placed, such as 1234 (it would be more difficult to get numbers with more than 1 place, for example, 10, 11...)
test1 = input('insert numbers :')
lst = [int(number) for number in test1]
lst
In this way, you perform the separation using comma (',') for the numbers, like 1,12,13,5 and I think this is more appropriate because you can get all numbers.
test2 = input('insert numbers (separate with comma):')
lst = [int(number) for number in test2.split(',')]
lst
So essentially I need a list in where I can iterate through numbers 1 to 48 . My coworkers will be inspecting my code so I don't want to be made fun of if I do this:
myList = [1, 2, 3, 4, 5, .....47, 48]
Is there any elegant way where I can create a (sorted) list consisting of numbers 1 - 48 ?
I appreciate the help!
python - How do I create a list with numbers between two values? - Stack Overflow
How to create a list of numbers from 1 to 48 without hardcoding it ?
Creating a list of numbers containing 1 to 10 using the range() function and/ or a for loop
List of ordinal numbers using an if-elif-else chain
Use range. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.
To increment by steps of 0.5, consider using numpy's arange() and .tolist():
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
You seem to be looking for range():
>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
For incrementing by 0.5 instead of 1, say:
>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]