Reputation: 605
I have a list that must only contain ints, how would i proceed to control every element of the list and return a message if it is not an int?
Could i for instance somehow use isdigit()?
Thanks
Upvotes: 2
Views: 133
Reputation: 31192
Why don't you use Array in such case?
From docs:
Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.
Upvotes: 0
Reputation: 177875
You can use all
and the abstract base class Number
:
>>> all(isinstance(x, Number) for x in mylist)
Upvotes: 7
Reputation: 11804
You can check if all elements of a list are of a particular type:
all(map(lambda x: type(x) == type(0), list))
This will return False iif there is at least one element of list
which is not of the same type as 0. You can change the condition to fit your needs, depending on whether you need integers or reals or anything else.
Upvotes: -1