How to emulate sum() using a list comprehension in python?

[2559 views]




Do you know to emulate sum() using a list comprehension? There are many programmers who still don’t know about this. Don’t feel bad about that because we are going to cover almost everything about emulating sum () using a list comprehension in python.

Before you should learn to know about emulating sum(), it is necessary to learn something about list comprehension.

What is list comprehension?

In python, comprehension allows us to create new sequences easily with the existing sequence. There are four types of comprehension in python- list comprehension, dictionary comprehension, set comprehension and generator comprehension. List comprehension offer a simple way for creating new lists. It always provides results without even using results or nor. You can iterate and manipulate variable simultaneously.

Problem Statement

Suppose you have to compute the product of all elements in the list. There are two ways to do so. For instance:

list = [1, 2, 3] product = [magic_here for i in list] #product is expected to be 6

Another code that do the same:

def product_of(input): result = 1 for i in input: result *= i return result

Solution

The fact is it is not possible to emulate because a list comprehension generates a list that with the same length as the input. You have to use other functional tools for aggregating the sequence into a single value.

In the case of functional programming, aggregate is also known as a fold, reduce, accumulate, compress or inject. It is a group of the higher-order functions used for analyzing a recursive data structure. However, it is of no use to combine them and provide a single value. Let’s see various solutions for solving your problem.

In python 2.5 / 2.6

You can use
vars()['_[1]']
pointing out to the list comprehension being made currently. However, as said before, it can make your code dreadful. According to your question, this is what you can do here only.
>>> nums = [1, 2, 3] >>> [n * (vars()['_[1]'] or [1])[-1] for n in nums][-1] 6

In python 3

You can try to emulate sum() with the help of reduce functionality. Use the following code:

>>> from functools import reduce >>> from operator import mul >>> nums = [1, 2, 3] >>> reduce(mul, nums) 6

In the latest version of Python 3.8, programmers can use walrus operator to increment a variable and reducing the list to the total sum of its elements.

total = 0 [total := total + x for x in [1, 2, 3]] # 6

In the above code, it initializes the total variable to 0. For every item in the list, the variable total is incremented by making the use of current looped item and walrus operator.

Using Lambda and Reduce

The problem can also be solved as given in the given code. This is the beauty of programming. You can solve the same problem using a number of different methods.

a = [1,2,3] >>> reduce(lambda x, y: x*y, a) 6

Hopefully, you have understood and learned all the ways to emulate sum () using list comprehension.

                 






Comments










Search Anything:

Sponsored Deals ends in






Search Tags