How to add numbers in a list together in python

x = [2, 4, 7, 12, 3]
sum_of_all_numbers= sum[x]

or you can try this:

x = [2, 4, 7, 12, 3] 
sum_of_all_numbers= reduce[lambda q,p: p+q, x]

Reduce is a way to perform a function cumulatively on every element of a list. It can perform any function, so if you define your own modulus function, it will repeatedly perform that function on each element of the list. In order to avoid defining an entire function for performing p+q, you can instead use a lambda function.

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]]]]


    How do you add numbers in a list in Python?

    Python Example add numbers in a list.
    Use list.append[].
    Output: [1, 2, 3, 4].
    Use enumerate[].
    Output: [2, 2, 3].
    Output:.

    How do I add two numbers in a list in Python?

    Practical Data Science using Python.
    Take two lists l1 and l2. Initialize head and temp as null..
    c := 0..
    while l1 and l2 both are non-empty lists. if l1 is non-empty, then set a := 0, otherwise set a := l1.val. ... .
    if c is non-zero, then. node := new node with value 1, next of head := node..
    return temp..

    How do you sum parts of a list in Python?

    Sum Of Elements In A List Using The sum[] Function. Python also provides us with an inbuilt sum[] function to calculate the sum of the elements in any collection object. The sum[] function accepts an iterable object such as list, tuple, or set and returns the sum of the elements in the object.

    How do you merge a list of numbers in Python?

    Use the join[] method of Python. First convert the list of integer into a list of strings[ as join[] works with strings only]. Then, simply join them using join[] method. It takes a time complexity of O[n] .

    Chủ Đề