user3926579
user3926579

Reputation: 67

Processing a list to return true/false values using a for loop - Python

I have written this function trying to get it to return true or false in terms of the following condition, however I get this result when testing it:

>>> has_gt([2,3,4], 2) False

def has_gt(nums, n):
"""Return True iff nums contains at least one number bigger than n.

has_gt(list<number>, number) -> boolean
"""
for i in (nums, n):
    if i in nums > n:
        return True
    else:
        return False

Upvotes: 1

Views: 7348

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

def has_gt(myList, value):
    return any(i > value for i in myList)

>>> has_gt([2,3,4], 3)
True
>>> has_gt([1,2,3], 7)
False

Using a for loop

def has_gt(myList, value):
    for i in myList:
        if i > value:
            return True
    return False

Upvotes: 3

Related Questions