How do you verify that the type of all elements in a list or a tuple are the same and of a certain type?
for example:
(1, 2, 3) # test for all int
all(isinstance(n, int) for n in lst)
Demo:
In [3]: lst = (1,2,3) In [4]: all(isinstance(n, int) for n in lst) Out[4]: True In [5]: lst = (1,2,'3') In [6]: all(isinstance(n, int) for n in lst) Out[6]: False
Instead of isinstance(n, int) you could also use type(n) is int
isinstance(n, int)
type(n) is int