**Article summary:** Here’s a quick visual overview of the contents of this tutorial.

**Flat List:**To sum over a list of numbers in a single line of Python code, use Python’s built-in function`sum(list)`

.**Nested List:**To sum over a list of lists in one line Python, use a generator expression to flatten the list and pass the result into the function:`sum(x for y in list for x in y)`

.

## Method 1: Sum over a Flat List in One Line

**Problem**: How to sum over all values in a given Python list?

**Example**: Given the following list.

a = [1, 2, 3]

You want to calculate the sum of all values in the list—using only a single line of Python code!

# RESULT: 6

**Solution**: Python’s built-in `sum()`

function helps you to sum over all values in an iterable, such as a Python list.

Summing up a list of numbers appears everywhere in coding. Fortunately, Python provides the built-in `sum()`

function to sum over all elements in a Python list—or any other iterable for that matter. (Official Docs)

**Code**: Here’s the minimal code example.

a = [1, 2, 3] print(sum(a)) # 6

How does it work? The syntax is `sum(iterable, start=0)`

:

Argument | Description |
---|---|

`iterable` | Sum over all elements in the `iterable` . This can be a list, a tuple, a set, or any other data structure that allows you to iterate over the elements. Example: `sum([1, 2, 3])` returns `1+2+3=6` . |

`start` | (Optional.) The default start value is 0. If you define another start value, the sum of all values in the `iterable` will be added to this start value. Example: `sum([1, 2, 3], 9)` returns `9+1+2+3=15` . |

**Exercise**: Try to modify the sequence so that the sum is 30 in our interactive Python shell:

## Method 2: Sum over a Nested List of Lists in One Line

**Problem**: Given multiple lists in a list of lists. How can you sum over all values in a list of lists such as `[[1, 2], [3, 4], [5, 6]]`

in Python?

**Solution**: Use a generator expression to flatten the values in the nested list and pass the resulting iterable into the `sum()`

function.

**Code**: The following code creates a list of lists:

a = [[1, 2], [3, 4], [5, 6]]

To sum over the values in the list of lists, use the following one-liner:

print(sum(x for y in a for x in y))

The output is printed on the shell:

# OUTPUT: 21

But how does it work? The main part of the code is the generator expression `x for y in a for x in y`

that flattens the list.

- The part
`x`

iterates over all elements**for y in a**for x in y`y`

in the nested list`a`

. - The part
`x for y in a`

iterates over all elements**for x in y**`y`

in the inner list`y`

. - The part

stores the inner element in the iterable.**x**for y in a for x in y

Here’s a recap on the technique of list comprehension.

To learn more about different ways to sum() elements in a list, check out my detailed blog tutorial:

**Related Tutorial**: Python sum() List — Ultimate Guide

## Python One-Liners Book

**Python programmers will improve their computer science skills with these useful one-liners.**

*Python One-Liners* will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:

**•** Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution**•** Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics**•** Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning**•** Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators**•** Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.