Isinstance on list types

nums = ['1', '2', '3', '4', '5'] 
y = list(map(int, nums)) 
x = isinstance(y, list)
print(x)

The above code works, it returns True that it is a list.

So I wanted to see if isinstance could tell me if a list has been successfully converted to int. But when I try, it does not return true as expected.

nums = ['1', '2', '3', '4', '5'] 
y = list(map(int, nums)) 
x = isinstance(y, int)
print(x)

if map has applied int to each iteration in the list called nums, and put them in a new list called x.
Then instanceof should recognise x as an int?

Thanks
Kyle

Hello @cloud0675654875, welcome to the forums!
While it is true that y will contain a list of integers (because you’ve applied the map function), the variable itself is still a list (in Python, there isn’t a type difference between a list of integers and a list of strings).


As an aside, the variable x will be neither a list nor an int; it’s a boolean.

I hope this helps!

2 Likes

This does help, but seems to be a weakness in python.
How do i determine if a thread is comprised on int or strings?
iterating through them individually might be possible on a small scale, but what about larger lists etc.

Yeah; that is an issue with dynamically typed languages, but looping through larger lists is only O(n) time anyway—so it shouldn’t add too much to a program. I did find this StackOverflow, which talks about ways to check an entire list for one (or more) types of element, and generally references the all() function, which is still O(n).

1 Like