Reputation: 3
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
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))
[10]
The output is a list of the values in numList
that are greater or equal to the comparison integer.
Upvotes: 1
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