vurp0
vurp0

Reputation: 251

How to test if every item in a list of type 'int'?

Say I have a list of numbers. How would I do to check that every item in the list is an int?
I have searched around, but haven't been able to find anything on this.

for i in myList:
  result=isinstance(i, int)
  if result == False:
    break

would work, but looks very ugly and unpythonic in my opinion.
Is there any better(and more pythonic) way of doing this?

Upvotes: 25

Views: 47484

Answers (6)

AnalyticsBuilder
AnalyticsBuilder

Reputation: 4261

There are a few different ways to do it. For example, if your list includes only numbers:

>>> my_list = [1, 2, 3.25]
>>> all(isinstance(item, int) for item in my_list)
False

>>> other_list = range(3)
>>> all(isinstance(item, int) for item in other_list)
True
>>> 

Anyways, this solution doesn't work as expected if your list includes booleans, as remarked by @merlin:

>>> another_list = [1, 2,False]
>>> all(isinstance(item, int) for item in another_list)
True

If your list include booleans you should use type instead of isinstance (it' a little slower, but works as you expect):

>>> another_list = [1, 2, False]
>>> all(type(item) is int for item in another_list)
False
>>> last_list = [1, 2, 3]
>>> all(type(item) is int for item in last_list)
True

Upvotes: 56

Dennis
Dennis

Reputation: 111

Found myself with the same question but under a different situation: If the "integers" in your list are represented as strings (e.g., as was the case for me after using 'line.split()' on a line of integers and strings while reading in a text file), and you simply want to check if the elements of the list can be represented as integers, you can use:

all(i.isdigit() for i in myList)

For example:

>>> myList=['1', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList)
True

>>> myList2=['1.5', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList2)
False

Upvotes: 1

Shameem
Shameem

Reputation: 2814

See functions

def is_int(x):
    if type(x) == int:
        return True
    return


def all_int(a):
    for i in a:
        if not is_int(i):
            return False
    return True

Then call

all_int(my_list) # returns boolean

Upvotes: 1

senderle
senderle

Reputation: 150977

One approach would not be to test, but to insist. This means your program can handle a broader range of inputs intelligently -- it won't fail if someone passes it a float instead.

int_list = [int(x) for x in int_list]

or (in-place):

for i, n in enumerate(int_list):
    int_list[i] = int(n)

If something can't be converted, it will throw an exception, which you can then catch if you care to.

Upvotes: 3

Anil Shanbhag
Anil Shanbhag

Reputation: 968

In [1]: a = [1,2,3]

In [2]: all(type(item)==int for item in a)
Out[2]: True

Upvotes: 1

WirthLuce
WirthLuce

Reputation: 1391

The following statement should work. It uses the any builtin and a generator expression:

any(not isinstance(x, int) for x in l)

This will return true if there is a non-int in the list. E.g.:

>>> any(not isinstance(x, int) for x in [0,12.])
True
>>> any(not isinstance(x, int) for x in [0,12])
False

The all builtin could also accomplish the same task, and some might argue it is makes slightly more sense (see Dragan's answer)

all(isinstance(x,int) for x in l)

Upvotes: 9

Related Questions