DEV Community

Discussion on: Python Map Function

Collapse
 
gioragutt profile image
Giora Guttsait • Edited

Very cute! The first example also made me realise that map, filter, and the likes, that work on iterables, are lazy.

def newfunc(val):
  print('lazy ' + str(val)) # this is proof for lazy
  return val * 2

result = map(newfunc, [1, 2, 3, 4]) 
print(result) # prints the map object, but the newfunc operation
              # was not invoked on any of the values
print(next(result)) # only invokes newfunc on 1
print(list(result)) # invokes newfunc on the rest

repl.it example

Collapse
 
thepythongeeks profile image
Python Geeks • Edited

Awesome! It's an interesting thing that I haven't known yet. Thanks a lot for your sharing.
Btw, Don't you mind if I share your example in the original post? It will help other developers more understand map and filter.

Collapse
 
gioragutt profile image
Giora Guttsait

Also, notice that my code example has syntax highlighting.

This is because I use

```python
def some_method():
    pass
```

Instead of

```
def some_method():
    pass
```
Thread Thread
 
thepythongeeks profile image
Python Geeks

Thanks for the notice. It helps me a lot. :)

Collapse
 
gioragutt profile image
Giora Guttsait

I most certainly don't mind :)