Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

summing only the numbers contained in a list

Give a method that sums all the numbers in a list. The method should be able to skip elements that are not numbers. So, sum([1, 2, 3]) should be 6 but sum(['A', 1, 'B', 2, 3]) should also be 6. How can I accomplish this?

What I have already tried so far:

def foo(list):
    dict = "ABCDEFGHIJKLMN"
    n = 0
    for i in range(0, len(list) - 1):
        if list[i].str in dict:
            ""
        else:    
            n= n + list[i]
    return n

print foo([1, 2, 3, 4, 5, 6, "A", "B"])
like image 765
user3419487 Avatar asked Jan 11 '16 18:01

user3419487


People also ask

How do you sum only even numbers in a list Python?

In this python program to find Sum of Even and Odd Numbers in a List, User entered items = [2, 3, 4, 5], Even_Sum = 0, Odd_Sum = 0. if(NumList[1] % 2 == 0) => if(3 % 2 == 0) – Condition is False, so it enters into the Else block. if(5 % 2 == 0) – Condition is False, so it enters into Else block.

How do you sum all numbers in a list in Python?

sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.


Video Answer


2 Answers

You can do this with a simple one liner:

l1 = [1, 2, 3, 'A']

sum(filter(lambda i: isinstance(i, int), l1))
# prints 6

Or, if you need it inside a function:

def foo(l1):
    return sum(filter(lambda i: isinstance(i, int), l1))

Additionally, as noted in the comments, don't use names like dict and list for your variables; *they will shadow they build-in names for the dictionary (dict) and (list) types. You'll then need to explicitly del dict, list in order to use them as intended.


But, let me explain. What filter does is here is:

a) It takes a function as its first argument:

# this function will return True if i is an int
# and false otherwise
lambda i: isinstance(i, int)

and then takes every element inside the list l1 (second argument) and evaluates whether it is True or False based on the function.

b) Then, filter will essentially filter out any objects inside list l1 that are not instances of int (i.e the function returns False for them). As a result, for a list like [1, 2, 3, 'A'] filter is going to return [1, 2, 3] which will then be summed up by sum().

Some Examples:

foo([1, 2, 3, 'A'])
# 6

foo([1, 2, 3])
# 6

foo([1, 2, 3, 'HELLO', 'WORLD'])
# 6

Slight caveat:

As is, this doesn't sum up float values, it drops them (and any other numeric types for that case). If you need that too, simply add the float type in the lambda function as so:

lambda i: isinstance(i, (int, float))

Now, your function sums floats too:

foo([1, 2, 3, 3.1,  'HELLO', 'WORLD'])
# 9.1

Add any other types as necessary in the lambda function to catch the cases that you need.


A catch all case:

As noted by @Copperfield you can check for objects that are instances of any number by utilizing the numbers.Number abstract base class in the numbers module. This acts as a catch-all case for numeric values:

import numbers # must import
sum(filter(lambda i: isinstance(i, numbers.Number), l1))

Simpler and a bit faster, too:

Additionally, as noted by @ShadowRanger, and since lambda might not be the most comfortable construct for new users, one could simply use a generator expression (which is also faster) with sum to get the same exact result:

sum(val for val in l1 if isinstance(val, numbers.Number))
like image 110
Dimitris Fasarakis Hilliard Avatar answered Oct 19 '22 15:10

Dimitris Fasarakis Hilliard


The Pythonic way is to do a try/except. While you could do this in a one liner, I prefer to break things out a bit to see exactly what is happening.

val=0
for item in list:
    try:
        val+=int(item)
    except ValueError:
        pass

If you want to include floating points, simply change the int to a float. Floating points are anything with a decimal, among others.

like image 11
PearsonArtPhoto Avatar answered Oct 19 '22 15:10

PearsonArtPhoto