Issue
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 = True
(1, 3, 'a') # test for all int = False
Solution
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
Answered By - ThiefMaster
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.