The
""
format you mean, is a
str
.
If you have this list with strings:
some_values = ["1", "-5", "10", "-15"]
You can cast the
str
to
int
with different methods.
Some examples:
# the values defined here
some_values = ["1", "-5", "10", "-15"]
# list comprehension
new_some_values_1 = [int(value) for value in some_values]
# classical for-loop
new_some_values_2 = [] # empty list
for value in some_values:
value = int(value)
new_some_values_2.append(value)
# functional style with map
# each element of some_values is called with int()
# the list consumes the resulting map object
# it's lazy evaluated, not consuming the map object -> no values
new_some_values_3 = list(map(int, some_values))
# generator expression + for-loop
values = (int(value) for value in some_values)
for value in values: # <- evaluation of the generator begins here
print(value, type(value)) # <- printing the value and type of value
# now values is consumed
# you can't use it again because it's now empty