0

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))
2
  • 1
    numList is a list right now, and comparison is an int - do you know how to get an int out of the list numList? Commented Oct 2, 2017 at 2:32
  • You seem to be mixing up your variables, did you mean print(numCount(numList, 9)) and if someList >= comparison:. Note: You can do this with filter(), e.g. return list(filter(lambda x: x >= comparison, someList)) Commented Oct 2, 2017 at 2:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, sir. As a beginner, this helped me greatly. Upvoted^
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.