Python: Test if value can be converted to an int in a list comprehension

If you only deal with integers, you can use str.isdigit():

Return true if all characters in the string are digits and there is at least one character, false otherwise.

[row for row in listOfLists if row[x].isdigit()]

Or if negative integers are possible (but should be allowed):

row[x].lstrip('-').isdigit()

And of course this all works only if there are no leading or trailing whitespace characters (which could be stripped as well).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)