This question already has an answer here:
- Convert all strings in a list to int 3 answers
How do I convert a space separated integer input into a list of integers?
Example input:
list1 = list(input("Enter the unfriendly numbers: "))
Example conversion:
['1', '2', '3', '4', '5'] to [1, 2, 3, 4, 5]
this works:
You can try:
map()
is your friend, it applies the function given as first argument to all items in the list.since it maps every iterable, you can even do:
which (in python3.x) returns a map object, which can be converted to a list. I assume you are on python3, since you used
input
, notraw_input
.One way is to use list comprehensions:
Say there is a list of strings named list_of_strings and output is list of integers named list_of_int. map function is a builtin python function which can be used for this operation.