Summing Elements in a List

Summing elements in a list

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.

Sum of a list 2 elements by 2 elements

You could iterate over the list with steps of 2.

orig_len = len(tab)  # List length
tab1 = []
for i in range(0, orig_len - orig_len%2, 2):
tab1.append(tab[i] + tab[i+1])
print(tab1)

You have to round down the number to first smaller even number, which is that orig_len - orig_len%2 does.

Summing elements of list of lists in Python

A very simple way to solve this, is to maintain a total variable and replace the second value of each sub-list with this sum.

myList = [[8100, 3], [8200, 5], [8400, 8]]
total, new_list = 0, []

for sublist in myList:
total += sublist[1]
new_list.append([sublist[0],total])

print(new_list)

Output

[[8100, 3], [8200, 8], [8400, 16]]

How to sum up elements in list in a moving range

Try this (lst being your list and n being your range):

print(max(sum(lst[i:i+n+1]) for i in range(len(lst)-n)))

So for example:

>>> lst = [5.8, 1.2, 5.8, 1.0, 6.9, 0.8, 6.0, 18.4]
>>> n = 5
>>> print([sum(lst[i:i+n+1]) for i in range(len(lst)-n)])
[21.5, 21.7, 38.9]
>>> print(max(sum(lst[i:i+n+1]) for i in range(len(lst)-n)))
38.9

Summing elements in list of lists in Python

Use a defaultdict:

import collections

# by default, non-existing keys will be initialized to zero
myDict = collections.defaultdict(int)

for key, value in myList:
myDict[key] += value

# transform back to list of lists
myResult = sorted(list(kv) for kv in myDict.items())

I need to get the sum of each element from a list

Each list's item (in your case: each inner list) can be accessed by using item (see here, but also know about first and last) or as the current element of foreach (see here).

Here a bunch of ways with which you can accomplish your goal. First showing simply how to operate on each inner list, then showing how to directly build a list containing each inner list's sum:

to play-with-lists
print "The original list:"
let list-of-lists [[0 1 2] [4 6 9] [-1 0 3]]
print list-of-lists

print "Manually reporting sums:"
print sum item 0 list-of-lists
print sum item 1 list-of-lists
print sum item 2 list-of-lists

print "Building a list of sums with while loop using 'while':"
let list-of-sums []
let i 0
while [i < length list-of-lists] [
set list-of-sums lput (sum item i list-of-lists) (list-of-sums)
set i i + 1
]
print list-of-sums

print "Building a list of sums with for loop using 'foreach':"
set list-of-sums []
foreach list-of-lists [
inner-list ->
set list-of-sums lput (sum inner-list) (list-of-sums)
]
print list-of-sums

print "Building a list of sums using 'map':"
print map sum list-of-lists
end

How to sum elements of N lists in python?

Simply this:

L = [(['a', 1], ['b', 2]), (['c', 3], ['d', 4], ['e', 5])]

for t in L:
s = ''.join(x[0] for x in t)
S = sum(x[1] for x in t)
print([s, S])

and refrain to define variable names using python keywords...



Related Topics



Leave a reply



Submit