Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the entire list of strings to integers is quite common in the development domain. Let’s discuss a few ways to solve this particular problem.
Method 1: Using eval[]
Python eval[] function parse the expression argument and evaluate it as a python expression and runs Python expression[code], If the expression is an int representation, Python converts the argument to an integer.
Python3
lis
=
[
'1'
,
'-4'
,
'3'
,
'-6'
,
'7'
]
res
=
[
eval
[i]
for
i
in
lis]
print
[
"Modified list is: "
, res]
Output:
Modified list is: [1, -4, 3, -6, 7]
Method 2: Naive Method
This is the most generic method that strikes any programmer while performing this kind of operation. Just looping over the whole list and converting each string of the list to int by type casting.
Python3
test_list
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
for
i
in
range
[
0
,
len
[test_list]]:
test_list[i]
=
int
[test_list[i]]
print
[
"Modified list is : "
+
str
[test_list]]
Output:
Modified list is: [1, 4, 3, 6, 7]
Method 3: Using list comprehension
This is just a kind of replica of the above method, just implemented using list comprehension, a kind of shorthand that a developer looks for always. It saves the time and complexity of coding a solution.
Python3
test_list
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
test_list
=
[
int
[i]
for
i
in
test_list]
print
[
"Modified list is : "
+
str
[test_list]]
Output:
Modified list is : [1, 4, 3, 6, 7]
Method 4: Using map[]
This is the most elegant, pythonic, and recommended method to perform this particular task. This function is exclusively made for this kind of task and should be used to perform them.
Python3
test_list
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
test_list
=
list
[
map
[
int
, test_list]]
print
[
"Modified list is : "
+
str
[test_list]]
Output:
Modified list is : [1, 4, 3, 6, 7]
Method 5: List of strings with mixed integer representations
Here, we will first convert each string to a float first and then we will convert it into an integer by using the round[] function, otherwise, it will through error.
Python3
lis
=
[
'1.1'
,
'4'
,
'3.5'
,
'6.7'
,
'7.2'
]
res
=
[
round
[
float
[i]]
for
i
in
lis]
print
[
"Modified list is: "
, res]
Output:
Modified list is: [1, 4, 4, 7, 7]
Split on commas, then map to integers:
map[int, example_string.split[',']]
Or use a list comprehension:
[int[s] for s in example_string.split[',']]
The latter works better if you want a list result, or you can wrap the map[]
call in list[]
.
This works because int[]
tolerates whitespace:
>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list[map[int, example_string.split[',']]] # Python 3, in Python 2 the list[] call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int[s] for s in example_string.split[',']]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
Splitting on just a comma also is more tolerant of variable input; it doesn't matter if 0, 1 or 10 spaces are used between values.