This is what you are looking for:

array[-1] * sum(array[::2])

array[::2] traverses the array from first index to last index in steps of two, i.e., every alternate number. sum(array[::2]) gets the sum of alternate numbers from the original list.

Using index will work as expected only when you are sure the list does not have duplicates, which is why your code fails to give the correct result.

Answer from shaktimaan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sum-elements-matching-condition
Python - Sum Elements Matching Condition - GeeksforGeeks
July 12, 2025 - Using list comprehension with sum() allows us to efficiently sum elements that meet a specific condition in a single line.
Discussions

How to get sum of elements lists with condition in python? - Stack Overflow
How to get sum of elements lists with condition in python ? a = [{ "a" : 1 }, {"a" : 1}, { "a" : 3 }, { "a" : 4 }, { "a" : 5 }, { "a" : 7 }] def test(d): cnt = 0 for row in d: if... More on stackoverflow.com
🌐 stackoverflow.com
March 15, 2016
bam - sum elements in python list if match condition - Stack Overflow
I think that makes it harder to ... the same list. ... What is the desired result with ['20', 'M', '10', 'M', '1', 'D', '2','D', '14', 'M', '106', 'M']? Are the consecutive 'D' entries to be summed? ... Hi @dawg, there will never be consecutive Ds in the data, the only reason there are consecutive Ms is because I need to change Ss to Ms. Consecutive letters are not allowed, that is why I need to add the Ms. ... You can slice Python lists using ... More on stackoverflow.com
🌐 stackoverflow.com
python - Find Sum of list with condition - Stack Overflow
I am stuck at a problem. I have a List L = [1,2,3,4,5,6,7,8,9] I want to find the sum of elements in lists such that, if the sum exceeds's 25. I want to get the previous sum. for eg. 1+2+3+4+5+6... More on stackoverflow.com
🌐 stackoverflow.com
August 22, 2017
python - Sum of values in list when condition is met - Stack Overflow
Two identities where a sum and a difference of two cubes equal a perfect square ... What to do if a former supervisor with whom I am writing a paper, stops answering for almost a year? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Finxter
blog.finxter.com › home › learn python blog › python sum() list – a simple illustrated guide
Python sum() List - A Simple Illustrated Guide - Be on the Right Side of Change
April 23, 2020 - Example: Say, you’ve got the list lst = [5, 8, 12, 2, 1, 3] and you want to sum over all values that are larger than 4. Solution: Use list comprehension to filter the list so that only the elements that satisfy the condition remain.
Top answer
1 of 3
3

You can slice Python lists using a step (sometimes called a stride), you can use this to get every second element, starting at index 1 (for the first letter):

>>> example = ['30', 'M', '1', 'D', '120', 'M']
>>> example[1::2]
['M', 'D', 'M']

The [1::2] syntax means: start at index 1, go on until you run out of elements (nothing entered between the : delimiters), and step over the list to return every second value.

You can do the same thing for the numbers, using [::2], so begin with the value right at the start and take every other value.

If you then combine this with the zip() function you can pair up your numbers and letters to figure out what to sum:

def sum_m_values(values):
    summed = []
    m_sum = 0
    for number, letter in zip(values[::2], values[1::2]):
        if letter != "M":
            if m_sum:
                summed += (str(m_sum), "M")
                m_sum = 0
            summed += (number, letter)
        else:
            m_sum += int(number)
    if m_sum:
        summed += (str(m_sum), "M")
    return summed

The above function takes your list of numbers and letters and:

  • creates a list for the results
  • tracks a running sum of "M" values
  • pairs up the numbers and letters
  • for each pair:
    • if it is a number and "M", add that value (as an integer) to the running sum.
    • otherwise, adds the running sum (if any) to the list with the letter "M", then adds the current number and letter too.
  • after all pairs are processed, adds the running sum and the letter "M", if there is any.

This covers all your example inputs:

>>> def sum_m_values(values):
...     summed = []
...     m_sum = 0
...     for number, letter in zip(values[::2], values[1::2]):
...         if letter != "M":
...             if m_sum:
...                 summed += (str(m_sum), "M")
...                 m_sum = 0
...             summed += (number, letter)
...         else:
...             m_sum += int(number)
...     if m_sum:
...         summed += (str(m_sum), "M")
...     return summed
...
>>> examples = [
...     ['20', 'M', '10', 'M', '1', 'D', '14', 'M', '106', 'M'],
...     ['124', 'M', '19', 'M', '7', 'M'],
...     ['19', 'M', '131', 'M'],
...     ['3', 'M', '19', 'M', '128', 'M'],
...     ['12', 'M', '138', 'M'],
... ]
>>> for example in examples:
...     print(example, "->", sum_m_values(example))
...
['20', 'M', '10', 'M', '1', 'D', '14', 'M', '106', 'M'] -> ['30', 'M', '1', 'D', '120', 'M']
['124', 'M', '19', 'M', '7', 'M'] -> ['150', 'M']
['19', 'M', '131', 'M'] -> ['150', 'M']
['3', 'M', '19', 'M', '128', 'M'] -> ['150', 'M']
['12', 'M', '138', 'M'] -> ['150', 'M']

There are other methods of looping over a list in fixed-sized groups; you can also create an iterator for the list with iter()and then use zip() to pull in consecutive elements into pairs:

it = iter(inputlist)
for number, letter in zip(it, it):
    # ...

This works because zip() gets the next element for each value in the pair from the same iterator, so "30" first, then "M", etc.:

>>> example = ['124', 'M', '19', 'M', '7', 'M']
>>> it = iter(example)
>>> for number, letter in zip(it, it):
...     print(number, letter)
...
124 M
19 M
7 M

However, for short lists it is perfectly fine to use slicing, as it can be understood more easily.

Next, you can make the summing a little easier by using the itertools.groupby() function to give you your number + letter pairs as separate groups. That function takes an input sequence, and a function to produce the group identifier. When you then loop over its output you are given that group identifier and an iterator to access the group members (those elements that have the same group value).

Just pass it the zip() iterator build before, and either lambda pair: pair[1] or operator.itemgetter(1); the latter is a little faster but does the same thing as the lambda, get the letter from the number + letter pair.

With separate groups, the logic starts to look a lot simpler:

from itertools import groupby
from operator import itemgetter

def sum_m_values(values):
    summed = []
    it = iter(values)
    paired = zip(it, it)

    for letter, grouped in groupby(paired, itemgetter(1)):
        if letter == "M":
            total = sum(int(number) for number, _ in grouped)
            summed += (str(total), letter)
        else:
            # add the (number, "D") as separate elements
            for number, letter in grouped:
                summed += (number, letter)
            
    return summed

The output of the function hasn't changed, only the implementation.

Finally, we could turn the function into a generator function, by replacing the summed += ... statements with yield from ..., so it'll still generate a sequence of numeric strings and letters:

from itertools import groupby
from operator import itemgetter

def sum_m_values(values):
    it = iter(values)
    paired = zip(it, it)

    for letter, grouped in groupby(paired, itemgetter(1)):
        if letter == "M":
            total = sum(int(number) for number, _ in grouped)
            yield from (str(total), letter)
        else:
            # add the (number, "D") as separate elements
            for number, letter in grouped:
                yield from (number, letter)

You can then use list(sum_m_values(...)) to get a list again, or just use the generator as-is. For long inputs, that could be the preferred option as that means you never need to keep everything in memory all at once.

If you can guarantee that only numbers with M repeat (so a D pair is always followed by an M pair or is the last pair in the sequence), you can even just drop the if test and just always sum:

from itertools import groupby
from operator import itemgetter

def sum_m_values(values):
    it = iter(values)
    paired = zip(it, it)

    for letter, grouped in groupby(paired, itemgetter(1)):
        yield str(sum(int(number) for number, _ in grouped))
        yield letter

This works because there will only ever be one number value per D group, summing won’t make that into a different number.

2 of 3
0

solution using itertools package:

>>> from itertools import groupby, chain
>>> records = [
...     ['20', 'M', '10', 'M', '1', 'D', '14', 'M', '106', 'M'],
...     ['124', 'M', '19', 'M', '7', 'M'],
...     ['19', 'M', '131', 'M'],
...     ['3', 'M', '19', 'M', '128', 'M'],
...     ['12', 'M', '138', 'M'],
... ]
>>> res = []
>>> for rec in records:
...     res.append(list(
...         chain.from_iterable(
...             map(
...                 lambda x: (
...                     str(sum(map(lambda y: y[0], x[1]))),
...                     x[0],
...                 ),
...                 groupby(
...                     zip(map(int, rec[::2]), rec[1::2]),
...                     lambda k: k[1]
...                 )
...             )
...         )
...     ))
...
>>> res
[['30', 'M', '1', 'D', '120', 'M'], ['150', 'M'], ['150', 'M'], ['150', 'M'], ['150', 'M']]
🌐
Real Python
realpython.com › python-sum-function
Python's sum(): The Pythonic Way to Sum Values – Real Python
March 18, 2026 - The optional argument start can accept a number, list, or tuple, depending on what is passed to iterable. It can’t take a string. In the following two sections, you’ll learn the basics of using sum() in your code. Accepting any Python iterable as its first argument makes sum() generic, reusable, and polymorphic. Because of this feature, you can use sum() with lists, tuples, sets, range objects, and dictionaries:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python-summation-of-first-n-matching-condition
Python | Summation of first N matching condition | GeeksforGeeks
April 9, 2023 - We are having a list we need to ... matching condition is number should be even ). For example, n = [1, 2, 3, 4, 5, 6] so that output should be 12. Using List Comprehension with sum()Using list comprehension with sum() allows us to efficiently ...
🌐
YouTube
youtube.com › programgpt
python sum with condition - YouTube
Download this code from https://codegive.com Title: Conditional Summing in Python: A Comprehensive TutorialIntroduction:Summing elements in a list based on c...
Published: December 19, 2023
Views: 4
Top answer
1 of 3
1

Here's a basic, naive, FORTRAN like solution with your first data type:

int_and_floats = ['2', '4.384508781', '2', '1.38586366', '2', '25.4309252', '1', '9.969634146', '1', '10.3821918', '2', '70.02500521', '1', '12.21172958', '1', '13.53189471', '1', '6.166945117', '1', '16.28642897']

last_int = None
n = len(int_and_floats)
total = 0

for i in range(0, n, 2):
    a, b = int(int_and_floats[i]), float(int_and_floats[i + 1])
    total += b
    if a != last_int:
        total += a
    last_int = a

print(total)
# 175.775127174

With your second data format, you could just use groupby to chunk the ints together before summing them:

from itertools import groupby
ints = ['2', '2', '2', '1', '1', '2', '1', '1', '1', '1']
floats = ['4.384508781', '1.38586366', '25.4309252', '9.969634146', '10.3821918',
          '70.02500521', '12.21172958', '13.53189471', '6.166945117', '16.28642897']

print(sum(map(float, floats)) + sum(int(i) for i, _ in groupby(ints)))

And with numbers instead of strings, your code could be:

from itertools import groupby
ints = [2, 2, 2, 1, 1, 2, 1, 1, 1, 1]
floats = [4.384508781, 1.38586366, 25.4309252, 9.969634146, 10.3821918, 70.02500521, 12.21172958, 13.53189471, 6.166945117, 16.28642897]
print(sum(floats) + sum(i for i, _ in groupby(ints)))
# 175.775127174
2 of 3
0
a = ['2', '4.384508781', '2', '1.38586366', '2', '25.4309252', '1', 
     '9.969634146', '1', '10.3821918', '2', '70.02500521', '1', 
     '12.21172958', '1', '13.53189471', '1']

prev = 0
sm = 0
for each in a:
    if each.isdigit() and int(each)!=prev:
        sm += int(each)
        prev = int(each)
    elif '.' in each:
        sm += float(each)
print(sm)

should do the trick

🌐
Finxter
blog.finxter.com › home › learn python blog › python: how to count elements in a list matching a condition?
Python: How to Count Elements in a List Matching a Condition? - Be on the Right Side of Change
April 8, 2020 - I stumbled across this question ... can count the number of elements x that match a certain condition(x) by using the one-liner expression sum(condition(x) for x in lst)....
🌐
Stack Overflow
stackoverflow.com › questions › 38800497 › python-how-to-sum-up-values-in-list-by-condition
python how to sum up values in list by condition - Stack Overflow
May 9, 2017 - the only change I would make is in iterating over data, well over a enumerate of data, instead of range in the first for-loop and a slice of data in the second, but that make a copy of the list so I would leave the same or use islice ... Save this answer. ... Show activity on this post. The only way to make your code more "pythonic" is probably to use enumerate() instead of rang(len(object)). Other than that it's pretty much as pythonic as possible. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags.
🌐
Reddit
reddit.com › r/learnpython › sum of lists (python)
r/learnpython on Reddit: Sum of Lists (Python)
January 17, 2021 -

In this exercise your function will receive three parameters,

a list of integers, and two integers. It will add up all values

in the list that do not equal either of the two integers.

This is what I have:

def sumniout(nums, a, b):
    for num in nums:
        if num == a or num == b:
            total = sum(nums) - a - b
        else:
            total = sum(nums)
        return total
print(suminout([1,2,3,4], 1,2))       

Example of it working:

suminout([1, 2, 3, 4], 1, 2) -> 7

My code gives me the right results sometimes. For example, if I run a test on print(suminout([1,2,3,4,5], 1,1)) I get 13 when I should get 14. Any ideas to get me on the right track?

Top answer
1 of 6
3

You could construct a result dict where key is tuple of first two items in the original lists and value is list of numbers. Every time you add value to dict you could use get to either return existing element or given default value, in this case empty list.

Once you have the existing list and list to add you can use zip_longest with fillvalue to get numbers to sum from both lists. zip_longest returns tuples of length 2 containing one number from each list. In case one list is longer than other fillvalue is used as default so this will also work in case lists have different lengths. Finally list comprehension could used to sum each item for a new value:

from itertools import zip_longest

l = [
    ['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
    ['London','2014',4800,70,90],['Bern','2013',300,450,678],
    ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]
]

res = {}
for x in l:
    key = tuple(x[:2])
    res[key] = [i + j for i, j in zip_longest(res.get(key, []), x[2:], fillvalue=0)]

print(res)

Output:

{('Vienna', '2013'): [700, 850, 90], ('London', '2014'): [10200, 949, 168], 
 ('Vienna', '2012'): [890, 503, 70], ('Bern', '2013'): [800, 1150, 768]}  

If you want to sort the cities alphabetically and years latest first you could pass custom key to sorted:

for item in sorted(res.items(), key=lambda x: (x[0][0], -int(x[0][1]))):
    print(item)

Output:

(('Bern', '2013'), [800, 1150, 768])
(('London', '2014'), [10200, 949, 168])
(('Vienna', '2013'), [700, 850, 90])
(('Vienna', '2012'), [890, 503, 70])
2 of 6
2

You can achieve the result you want by simply using a dictionary store all the country names and years as one value. Each key in the dictionary is a tuple of the country name and the corresponding year.

Ex: key = (country,year).

This allows us to have the unique values that we need to group them by.

L = [
        ['Vienna','2012', 890,503,70],['London','2014', 5400, 879,78],
        ['London','2014',4800,70,90],['Bern','2013',300,450,678],
        ['Vienna','2013', 700,850,90], ['Bern','2013',500,700,90]
    ]

    countries = {}

    for list in L:
        key = tuple(list[0:2])
        values = list[2:]
        if key in countries:
            countries[key] = [sum(v) for v in zip(countries[key],values)]
        else:
            countries[key] = values

    print(countries)

out:

 {
     ('Vienna', '2012'): [890, 503, 70],
     ('London', '2014'): [10200, 949, 168],
     ('Bern', '2013'): [800, 1150, 768],
     ('Vienna', '2013'): [700, 850, 90]
}
Top answer
1 of 2
2

Let's make a test case:

In [59]: x = np.random.randint(0,10,10000)
In [60]: x.shape
Out[60]: (10000,)

(I thought test cases like this were required on Code Review. We like to have then on SO, and CR is supposed to be stricter about code completeness.)

Your code as a function:

def foo(pntl, adj_wgt, wgt_dif):
    sum_4s = 0
    for i in range(len(pntl)):
        if pntl[i] == 4 and adj_wgt[i] != 10:
           sum_4s += wgt_dif[i]
    return sum_4s

Test it with lists:

In [61]: pntl = adj_wgt = wgt_dif = x.tolist() # test list versions

In [63]: foo(pntl, adj_wgt, wgt_dif)
Out[63]: 4104
In [64]: timeit foo(pntl, adj_wgt, wgt_dif)
1000 loops, best of 3: 1.45 ms per loop

Same test with array inputs is slower (lesson - if you must loop, lists are usually better):

In [65]: timeit foo(x,x,x)
The slowest run took 5.44 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 3.97 ms per loop

The suggested list comprehension is modestly faster

In [66]: sum([w for w, p, a in zip(wgt_dif, pntl, adj_wgt) if p == 4 and a != 10])
Out[66]: 4104
In [67]: timeit sum([w for w, p, a in zip(wgt_dif, pntl, adj_wgt) if p == 4 and a != 10])
1000 loops, best of 3: 1.14 ms per loop

foo could have been written with zip instead of the indexed iteration. (todo - time that).

But since you say these are arrays, let's try a numpy version:

def foon(pntl, adj_wgt, wgt_dif):
    # array version
    mask = (pntl==4) & (adj_wgt != 10)
    return wgt_dif[mask].sum()

In [69]: foon(x,x,x)
Out[69]: 4104
In [70]: timeit foon(x,x,x)
10000 loops, best of 3: 105 µs per loop

This is an order of magnitude faster. So if you already have arrays, try to work with them directly, without iteration.


def foo2(pntl, adj_wgt, wgt_dif):
    sum_4s = 0
    for w, p, a in zip(wgt_dif, pntl, adj_wgt):
        if p == 4 and a != 10:
           sum_4s += w
    return sum_4s
In [77]: foo2(pntl, adj_wgt, wgt_dif)
Out[77]: 4104
In [78]: timeit foo2(pntl, adj_wgt, wgt_dif)
1000 loops, best of 3: 1.17 ms per loop

So it's the zip that speeds up your original code, not the list comprehension.

2 of 2
2
sum([w for w, p, a in zip(wgt_dif, pntl, adj_wgt) if p == 4 and a != max_wgt])

Explanation:

zip(a, b, c) 

creates the list of triplets of corresponding values from the lists a, b, c - something as

[(a[0], b[0], c[0]), (a[1], b[1], c[1]), (a[2], b[2], c[2]), ...]

so the part

for w, p, a in zip(wgt_dif, pntl, adj_wgt)

loops over this triples, associating th 1st item to w, 2nd to p, and 3rd to a.