Sum of specific elements in list python

You can sum numbers in a list simply with the sum[] built-in:

sum[your_list]

It will sum as many number items as you have. Example:

my_list = range[10, 17]
my_list
[10, 11, 12, 13, 14, 15, 16]

sum[my_list]
91

For your specific case:

For your data convert the numbers into int first and then sum the numbers:

data = ['5', '4', '9']

sum[int[i] for i in data]
18

This will work for undefined number of elements in your list [as long as they are "numbers"]

Thanks for @senderle's comment re conversion in case the data is in string format.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list of numbers, write a Python program to find the sum of all the elements in the list.

    Example:  

    Input: [12, 15, 3, 10]
    Output: 40
    Input: [17, 5, 3, 5]
    Output: 30

    Example #1: 

    Python3

    total = 0

    list1 = [11, 5, 17, 18, 23]

    for ele in range[0, len[list1]]:

        total = total + list1[ele]

    print["Sum of all elements in given list: ", total]

    Output

    Sum of all elements in given list:  74

    Example #2 : Using while[] loop  

    Python3

    total = 0

    ele = 0

    list1 = [11, 5, 17, 18, 23]

    while[ele < len[list1]]:

        total = total + list1[ele]

        ele += 1

    print["Sum of all elements in given list: ", total]

    Output: 
     

    Sum of all elements in given list:  74

    Example #3: Recursive way  

    Python3

    list1 = [11, 5, 17, 18, 23]

    def sumOfList[list, size]:

        if [size == 0]:

            return 0

        else:

            return list[size - 1] + sumOfList[list, size - 1]

    total = sumOfList[list1, len[list1]]

    print["Sum of all elements in given list: ", total]

    Output

    Sum of all elements in given list:  74

    Example #4: Using sum[] method  

    Python3

    list1 = [11, 5, 17, 18, 23]

    total = sum[list1]

    print["Sum of all elements in given list: ", total]

    Output: 

    Sum of all elements in given list:  74

    Example 5: Using add[] function of operator module

    First we have to import the operator module then using the add[] function of operator module adding the all values in the list. 

    Python3

    from operator import*

    list1 = [12, 15, 3, 10]

    result = 0

    for i in list1:

        result = add[i, 0]+result

    print[result]

    Method 6: Using enumerate function

    Python3

    list1 = [12, 15, 3, 10];s=0

    for i,a in enumerate[list1]:

      s+=a

    print[s]

    Method 7: Using list comprehension 

    Python3

    list1 = [12, 15, 3, 10]

    s=[i for i in list1]

    print[sum[s]]

    Method 8: Using lambda function

    Python3

    list1 = [12, 15, 3, 10]

    print[sum[list[filter[lambda x: [x],list1]]]]

    Method: Using add operator 

    Python3

    import operator

    list1 = [12, 15, 3, 10] ;s=0

    for i in list1:

      s=s+operator.add[0,i]

    print[s]


    How do you find the sum of certain elements in a list in Python?

    Syntax.
    Example: sum[list] sum[list, start].
    Code Example: # Python code to explain working on sum[] method # Declare list of numbers numlist = [2,4,2,5,7,9,23,4,5] numsum = sum[numlist] print['Sum of List: ',numsum] # Example with start numsum = sum[numlist, 5] print['Sum of List: ',numsum].

    How do you find the sum of two elements in a list in Python?

    Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

    How do you sum inputs in Python?

    How to Add Two Numbers in Python.
    ❮ Previous Next ❯.
    Example. x = 5. y = 10. print[x + y] Try it Yourself ».
    Example. x = input["Type a number: "] y = input["Type another number: "] sum = int[x] + int[y] print["The sum is: ", sum] Try it Yourself ».
    ❮ Previous Next ❯.

    How do you sum a list of strings in Python?

    To sum strings in Python:.
    Call the str. join[] method on an empty string..
    Pass the iterable [e.g. a list of strings] to the join[] method..
    The result will be a string containing the items of the iterable..

    Chủ Đề