Quantcast
Channel: User David Z - Stack Overflow
Viewing all articles
Browse latest Browse all 50

Answer by David Z for How do I sum the first value in each tuple in a list of tuples in Python?

$
0
0

In modern versions of Python I'd suggest what SilentGhost posted (repeating here for clarity):

sum(i for i, j in list_of_pairs)

In an earlier version of this answer I had suggested this, which was necessary because SilentGhost's version didn't work in the version of Python (2.3) that was current at the time:

sum([pair[0] for pair in list_of_pairs])

Now that version of Python is beyond obsolete, and SilentGhost's code works in all currently-maintained versions of Python, so there's no longer any reason to recommend the version I had originally posted.


Viewing all articles
Browse latest Browse all 50

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>