Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List comprehension as substitute for reduce() in Python

Tags:

The following python tutorial says that:

List comprehension is a complete substitute for the lambda function as well as the functions map(), filter() and reduce().

http://python-course.eu/python3_list_comprehension.php

However, it does not mention an example how a list comprehension can substitute a reduce() and I can't think of an example how it should be possible.

Can please someone explain how to achieve a reduce-like functionality with list comprehension or confirm that it isn't possible?

like image 426
F. Böller Avatar asked Jan 04 '16 06:01

F. Böller


People also ask

How do you reduce a list in Python?

Python offers a function called reduce() that allows you to reduce a list in a more concise way. The reduce() function applies the fn function of two arguments cumulatively to the items of the list, from left to right, to reduce the list into a single value.

What is comprehension equivalent in Python?

Practical Data Science using Python We can create new sequences using a given python sequence. This is called comprehension. It basically a way of writing a concise code block to generate a sequence which can be a list, dictionary, set or a generator by using another sequence.

How does list comprehension work in Python?

List comprehensions provide us with a simple way to create a list based on some sequence or another list that we can loop over. In python terminology, anything that we can loop over is called iterable. At its most basic level, list comprehension is a syntactic construct for creating lists from existing lists.

Is list comprehension faster than filter?

1 Answer. Actually, list comprehension is much clearer and faster than filter+lambda, but you can use whichever you find easier.


2 Answers

Ideally, list comprehension is to create a new list. Quoting official documentation,

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

whereas reduce is used to reduce an iterable to a single value. Quoting functools.reduce,

Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value.

So, list comprehension cannot be used as a drop-in replacement for reduce.

like image 185
thefourtheye Avatar answered Oct 10 '22 01:10

thefourtheye


I was surprised at first to find that Guido van Rossum, creator of Python, was against reduce. His reasoning was that beyond summing, multiplying, and-ing, and or-ing, using reduce yields an unreadable solution that is better suited by a function which iterates through and updates an accumulator. His article on the matter is here. So no, there isn't a list comprehension alternative to reduce, instead the "pythonic" way is to implement an accumulating function the old fashioned way:

Instead of:

out = reduce((lambda x,y: x*y),[1,2,3])

Use:

def prod(myList):     out = 1     for el in myList:         out *= el     return out 

Of course nothing stops you from continuing to use reduce (python 2) or functools.reduce (python 3)

like image 23
stackPusher Avatar answered Oct 10 '22 02:10

stackPusher