Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I have a tuple of tuples from a MySQL query like this:

T1 = (('13', '17', '18', '21', '32'), 

('07', '11', '13', '14', '28'), 

('01', '05', '06', '08', '15', '16'))

I'd like to convert all the string elements into integers and put them back into a list of lists:

T2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]]

I tried to achieve it with eval but didn't get any decent result yet.

1 Answer

0 votes
by (106k points)

You can use int() which is the Python standard built-in function to convert a string into an integer value. You call it with a string containing a number as the argument, and it returns the number converted to an integer:

print (int("1") + 1)

Structure of your list in Python 3:

T2 = [list(map(int, x)) for x in T1]

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...