isedbrrr
isedbrrr

Reputation: 3

Python strings and int

I've been following an online course and I've ran into a road block. I wanted to try to see if a list of integers is greater than one given integer. I keep getting the error TypeError: '>=' not supported between instances of 'list' and 'int'. Help?

Here's my attempt:

    def numCount(someList, comparison):
        returnVal = []
        if numList >= comparison:
                returnVal += numList
        return returnVal

    numList=[0, 2, 4, 5, 10]
    print(numCount(someList, 9))

Upvotes: 0

Views: 35

Answers (2)

Reblochon Masque
Reblochon Masque

Reputation: 36682

You must iterate over each item in the list and compare them:

def numCount(someList, comparison):
    returnVal = []
    for elt in someList:
        if elt >= comparison:
            returnVal.append(elt)
    return returnVal

numList=[0, 2, 4, 5, 10]
print(numCount(numList, 9))

output:

[10]

The output is a list of the values in numListthat are greater or equal to the comparison integer.

Upvotes: 1

JoyStickFanatic
JoyStickFanatic

Reputation: 22

Python does not support comparing a list to an int mainly because it doesnt make sense. Do you mean you want to see if the sum of a list of ints is larger than a given int? Or do you want to find all ints in a list that are larger than a given number and return a list containing those ints?

Upvotes: 0

Related Questions