You need to assign the converted items back to the sub-list (listy[0]
):
listy[0][:] = [int(x) for x in listy[0]]
Explanation:
for item in listy[0]:
int(item)
The above iterates over the items in the sub-list and converts them to integers, but it does not assign the result of the expression int(item)
to anything. Therefore the result is lost.
[int(x) for x in listy[0]]
is a list comprehension (kind of shorthand for your for
loop) that iterates over the list, converting each item to an integer and returning a new list. The new list is then assigned back (in place, optional) to the outer list.
This is a very custom solution for your specific question. A more general solution involves recursion to get at the sub-lists, and some way of detecting the candidates for numeric conversion.