53

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!

4
  • 2
    reduce, map, and filter :P unless you're in python3, in which case it's functools.reduce See here: docs.python.org/2/library/functions.html Commented Jun 30, 2015 at 0:53
  • Same naming I presume with being built-in functions. Commented Jun 30, 2015 at 0:54
  • Your last example is not the idiomatic/correct use of Array.prototype.map; you should instead use Array.prototype.forEach or ;[].forEach.call Commented Jun 30, 2015 at 1: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] Commented Jun 30, 2015 at 1:19

4 Answers 4

81

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is sort of what I'm looking for, but is it possible to include a function inside lambda?
Yes it is. eg. doublelength = lambda x: len(x)*2
8

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.

Comments

3
reduce(function, iterable[, initializer])

filter(function, iterable)

map(function, iterable, ...)

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

Comments

0

The first is:

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


word = ['Che', 'mis', 'try']
print(reduce(wordParts, word))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.