0

I'm trying to learn how to use the map() and filter() functions in Python but when I try and use them in visual studio, I'm getting an unusual output for each one. I know the code is probably wrong, but I can't see what it's outputting which is making it hard to sort out!

Thanks in advance

filter()

import functools

f = ["List", "of", "super", "crazily", "long", "words"]

new = lambda a, b: a if (len(a) > b) else b

print (filter(new, f))

Serves: filter object at 0x029AD5F0

map()

import functools

f = ["List", "of", "super", "crazily", "long", "words"]

map_loop = map((lambda x: len(x)), f)

print (type(map_loop), map_loop) 

Serves: class 'map', map object at 0x0243D5D0

Miles Monty
  • 41
  • 1
  • 6
  • https://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3-3-0 – behzad.nouri Dec 02 '14 at 13:02
  • Note that `filter` expects a `function` that takes *a single argument*; it's `functools.reduce` that requires a `function` with two arguments. – jonrsharpe Dec 02 '14 at 13:09
  • Thanks for this. Sorry it looks like I've been working from an old list of exercises online if these functions are pretty much obsolete now. Apologies for the dupe. – Miles Monty Dec 02 '14 at 21:18

1 Answers1

-1

you need to use list like this:

print (type(map_loop), list(map_loop)) 

demo:

>>> f = ["List", "of", "super", "crazily", "long", "words"]
>>> print(list(map(len,f)))     # no need of lembda,  `len` is enough
[4, 2, 5, 7, 4, 5]
Hackaholic
  • 19,069
  • 5
  • 54
  • 72