4

So my intention is to iterate over lists in lists with map, filter and lambda e.g.:

[ [33,3,3,0] , [34,1,0,4] ] should result in [ [33,3,3] , [34,1,4] ]

   def no_zeros(lst):
      a=map(filter(lambda x:(y==0 for y in x) ,lst),lst)
      print (list(a))
      #(says that filter is not callable)

I know that this code won't work but i do not know what exactly i do wrong since i'm still kind of new to lambda,etc.I already tried different stuff , like moving the filter after the lambda,but that did not seem to work either . Im gratefull for any kind of tipps or help.

5 Answers 5

9

map applies the function in first parameter to the second parameter. It tries to call your filter object...

A fixed version would be:

list(map(lambda z: list(filter(None,z)),lst))

(still not very clear even if it yields the expected result... it took me 3 or 4 tries to get it right by trial & error, and the fact that filter & map need to be iterated upon in Python 3 doesn't help and kills the whole hype about those fashionable keywords)

At this point of lambda/map/filter complexity you'd be better off with a list comprehension:

lst = [ [33,3,3,0] , [34,1,0,4] ]

result = [ [x for x in sl if x] for sl in lst]

print(result)

result:

[[33, 3, 3], [34, 1, 4]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , i did think that other methods,like list comprehension might be the better idea here , but i wanted to learn/try out more with lambda/map/filter. Thats why i chose to try it this way. Again thank all your help though.
2

The thing is that map expects a function/callable as a first argument but you're giving it the result of a filter.

You can fix this by applying filter to the sublists iterated by map:

def no_zeros(lst):
    a = map(lambda x: filter(lambda y: y != 0, x), lst)
    print(list(a))

By the way, you can do the same thing with list comprehensions:

def no_zeros(lst):
    a = [[y for y in x if y != 0] for x in lst]
    print(a)

1 Comment

Thanks , this actually helped me alot with the understanding of everything .
1
lst = list(map(lambda k: list(filter(lambda l: l != 0, k)), lst))

This should do the trick. You need apply a lambda on each element in the list through a filter. Be aware nested lambdas are ugly.

Comments

1

Here is one possibility:

import itertools
a = [ [33,3,3,0] , [34,1,0,4] ] 
list(map(list, map(filter, itertools.repeat(bool), a)))
# [[33, 3, 3], [34, 1, 4]]

Comments

1

Another solution might be:

def no_zeros(lst):
   if isinstance(lst, list):
      for i in lst:
          while 0 in i:
              i.remove(0)
      print(lst)
   else:
      raise TypeError("lst must be type of list")

2 Comments

careful! remove only removes the first occurrence of the element.
yeah you right, i did some changes to avoid this problem!

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.