How to filter the even and positive numbers from a list using functions

Question:

Given the list of integers stored in the variable numbers, declare the variable result1 and initialize the values ​​of the initial list containing only the even positive numbers, respecting the initial order.

numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95 .15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]

What I tried to do:

def filterList(p, xs):
    return [x for x in xs if p(x)]

def ePar(numbers):
    return numbers%2==0
    
def ePositive(numbers):
    return numbers>0
    
def result1():
    return filterList(eEven, ePositive)


print (result1[:7]) ## because i want the last seven even positive integers

I expected that with my function result1, it would filter just the even and positive integers, but instead, the error was the following:

***Run error***
Traceback (most recent call last):
  File "__tester__.python3", line 35, in <module>
    print(resultado1[:7])
TypeError: 'function' object is not subscriptable
Asked By: Vasco Ascensão

||

Answers:

I have no idea what your resultado1 or result1 variable is. I assume it to be the patent res variable used by programmers.

def filter_even_and_positive(num):
    return (num % 2 == 0 and num > 0)
res = []
for n in numbers:
    if filter_even_and_positive(n): res.append(n)
print(res[:7]) # First 7 values

With list comprehension you could shorten your code into:

res = [n for n in numbers if (n % 2 == 0 and n > 0)]
print(res[:7]) # First 7 Value

Output:

[34, 90, 36, 30, 100, 82, 88]
Answered By: The Myth

The problem with your code is that your filterList function takes a function and a list of ints as an argument (type annotations added below for clarity):

from typing import Callable

def filterList(p: Callable[[int], bool], xs: list[int]) -> list[int]:
    return [x for x in xs if p(x)]

but you’re calling it with two functions:

    return filterList(eEven, ePositive)   # two Callable[[int], bool]s

You could write your filterList to take an arbitrary number of functions instead, like this:

def filterList(xs: list[int], *p: Callable[[int], bool]) -> list[int]:
    return [x for x in xs if all(f(x) for f in p)]

and then call it like this:

def filterPositiveAndEven(xs: list[int]) -> list[int]:
    return filterList(xs, ePar, ePositive)

numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
print(filterPositiveAndEven(numbers))
# prints [34, 90, 36, 30, 100, 82, 88, 54, 58, 64, 50, 4, 32, 98, 76, 92, 52, 52, 52, 56, 34, 50, 94, 34, 50, 46, 88, 66, 92, 34, 66, 20, 28, 26, 94, 52, 14, 6]
Answered By: Samwise

You can conveniently use a generator for this as follows:

numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]

def even_pos(numlist):
    for v in numlist:
        if v > 0 and v % 2 == 0:
            yield v

print([*even_pos(numbers)][-7:]) # last 7 values

Output:

[20, 28, 26, 94, 52, 14, 6]
Answered By: OldBill
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.