I have a list containing strings. These strings are either words or integer values. For example this list might look like this:
['0', 'Negate', '-3', '2', 'SPECIALCASE', '3']
Now, based on their type (integer or string), I want to treat them differently. However, as you have noticed I can’t use isinstace(). I can still use type() and try to convert the integer values using the int() function and put the whole thing in a try-except method to avoid raising errors for the conversion of words. But this seems hackish to me. Do you know the right way of handling this case? Thanks in advance!
A pythonic way of “Don’t ask permission, ask for forgiveness”:
Note, that this should be done if you expect that only few items in the list are going to be the ‘special’ case items. Otherwise do the checks as @doomster advices.