Suppose,
var = ('x', 3)
How to check if a variable is a tuple with only two elements, first being a type str and the other a type int in python?
Can we do this using only one check?
I want to avoid this -
if isinstance(var, tuple):if isinstance (var[0], str) and (var[1], int):return True
return False
Here's a simple one-liner:
isinstance(v, tuple) and list(map(type, v)) == [str, int]
Try it out:
>>> def check(v):return isinstance(v, tuple) and list(map(type, v)) == [str, int]
...
>>> check(0)
False
>>> check(('x', 3, 4))
False
>>> check((3, 4))
False
>>> check(['x', 3])
False
>>> check(('x', 3))
True