41

What are Python's equivalent of the following (Javascript):

function wordParts (currentPart, lastPart) {
    return currentPart+lastPart;
}

word = ['Che', 'mis', 'try'];
console.log(word.reduce(wordParts))

and this:

var places = [
    {name: 'New York City', state: 'New York'},
    {name: 'Oklahoma City', state: 'Oklahoma'},
    {name: 'Albany', state: 'New York'},
    {name: 'Long Island', state: 'New York'},
]

var newYork = places.filter(function(x) { return x.state === 'New York'})
console.log(newYork)

lastly, this:

function greeting(name) {
    console.log('Hello ' + name + '. How are you today?');
}
names = ['Abby', 'Cabby', 'Babby', 'Mabby'];

var greet = names.map(greeting)

Thanks all!

Henry Lee
  • 731
  • 2
  • 8
  • 15
  • 2
    `reduce`, `map`, and `filter` :P unless you're in python3, in which case it's `functools.reduce` See here: https://docs.python.org/2/library/functions.html – NightShadeQueen Jun 30 '15 at 00:53
  • Same naming I presume with being built-in functions. – Iron Fist Jun 30 '15 at 00:54
  • Your last example is not the idiomatic/correct use of `Array.prototype.map`; you should instead use `Array.prototype.forEach` or `;[].forEach.call` – royhowie Jun 30 '15 at 01:09
  • 3
    Giant warning on the answers, here: list comprehensions and generators seem to be favored over `map` and `filter`, these days. So it looks like `[mutate(x) for x in list if x > 10]` – David Ehrmann Jun 30 '15 at 01:19

4 Answers4

71

They are all similar, lamdba functions are often passed as a parameter to these functions in Python.

Reduce:

 >>> from functools import reduce
 >>> reduce((lambda x, y: x + y), [1, 2, 3, 4])
 10

Filter:

>>> list(filter((lambda x: x < 0), range(-10,5)))
[-10, -9, -8, -7, - 6, -5, -4, -3, -2, -1]

Map:

>>> list(map((lambda x: x **2), [1,2,3,4]))
[1,4,9,16]

Docs

tinom9
  • 369
  • 2
  • 12
user3636636
  • 2,409
  • 2
  • 16
  • 31
5

It is worth noting that this question has been answered at face value above with the accepted answer, but as @David Ehrmann mentioned in a comment in the question, it is preferred to use comprehensions instead of map and filter.

Why is that? As stated in "Effective Python, 2nd Edition" by Brett Slatkin pg. 108, "Unless you're applying a single-argument function, list comprehensions are also clearer than the map built-in function for simple cases. map requires the creation of a lambda function for the computation, which is visually noisy." I would add the same goes for filter.

e.g. let's say I want to map and filter over a list to return the square of the items in the list, but only the even ones (this is an example from the book).

Using the accepted answer's method of using lambdas:

arr = [1,2,3,4]
even_squares = list(map(lambda x: x**2, filter(lambda x: x%2 == 0, arr)))
print(even_squares) # [4, 16]

Using comprehensions:

arr = [1,2,3,4]
even_squares = [x**2 for x in arr if x%2 == 0]
print(even_squares) # [4, 16]

So, along with others, I would advise using comprehensions instead of map and filter. This question dives into it even further.

As far as reduce goes, functools.reduce still seems like the proper option.

earl-95
  • 133
  • 3
  • 11
3
reduce(function, iterable[, initializer])

filter(function, iterable)

map(function, iterable, ...)

https://docs.python.org/2/library/functions.html

Sebastian Nette
  • 7,364
  • 2
  • 17
  • 17
0

The first is:

from functools import *
def wordParts (currentPart, lastPart):
    return currentPart+lastPart;


word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))
Adolfo Correa
  • 813
  • 8
  • 17