'How to convert a tuple list string value to integer

I have the following list:

l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]

I would like to converted it such that the tuple values are integers and not string. How do I do that?

Desired output:

[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]

What I tried so far?

l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]
new_list = []
        for i in `l:
            new_list.append((int(i[0]), i[1]))

        print(tuple(new_list))

This only converts the first element i.e. 15234, 15238, 15241, 15243 into int. I would like to convert all the values to int. How do I do that?



Solution 1:[1]

The easiest and most concise way is via a list comprehension:

>>> [tuple(map(int, item)) for item in l]
[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]

This takes each tuple in l and maps the int function to each member of the tuple, then creates a new tuple out of them, and puts them all in a new list.

Solution 2:[2]

You can change the second numbers into integers the same way you did the first. Try this:

new_list.append((int(i[0]), int(i[1]))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MattDMo
Solution 2