-2

Having error of not all arguments converted during string formatting

def even_odd_lambda(list_object):
  
    #odd_ctr = list(filter(lambda x: (x%2 != 0) , list_1))
    #even_ctr = list(filter(lambda x: (x%2 == 0) , list_1))
    
    odd_ctr = len(list(filter(lambda x: (x%2 != 0) , list_object)))
    even_ctr = len(list(filter(lambda x: (x%2 == 0) , list_object)))

    return [odd_ctr, even_ctr] 


if __name__ == '__main__':
  
    a=input("Add list elements seperated by space ").split(' ')

    
    
    output = even_odd_lambda(a)
    print("\nNumber of even numbers in the above array: ", output[1])
    print("\nNumber of odd numbers in the above array: ", output[0])  

enter image description here

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    `x` is a _string_, so `%` is [printf-style formatting](https://docs.python.org/3/library/stdtypes.html#old-string-formatting) not modulo. – jonrsharpe Oct 25 '21 at 10:13
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/q/4663306/6045800) – Tomerikoo Oct 25 '21 at 10:20

1 Answers1

1

Turn your input into integers:

a = [*map(int, input("Add list elements seperated by space ").split())]

also, you should avoid constructing lists for the sole purpose of measuring their length. I would also avoid using map/filter with lambda; it will always be longer and less readable than a generator or comprehension. Compare:

list(filter(lambda x: (x%2 != 0) , list_object))
[x for x in list_object if x%2 != 0]

You could, for instance, use sum with a generator to reduce the memory footprint and improve readability:

def even_odd_lambda(list_object):
    odd_ctr = sum(x%2 for x in list_object)
    even_ctr = sum(not x%2 for x in list_object) 
    return [odd_ctr, even_ctr] 
user2390182
  • 72,016
  • 6
  • 67
  • 89