# Sum of many lists

U9-Forward
commented

@d_kennetz he want's to simplify it

### 4 Answers

Try this:

```
a = [(0,0,0), (0,1,1), (1,0,1), (1,1,0)]
b = [0, 2, 1, 1, 2, 2]
x = 0
for index in b:
x += a[index]
print(x)
```

Which shortens to:

```
x = sum(a[index] for index in b)
```

Explanation of first method:

- create a variable to hold current sum
- for each index number specified in
`b`

, do the following:- add
`a[index]`

to the sum

- add

So on each loop cycle, `a[index]`

becomes `a[0]`

, `a[2]`

, `a[1]`

... `a[2]`

In the `sum`

method, those cycles are added together as the function takes the generator expression and exhausts it.

N Chauhan
posted this

Try this, need second argument for `sum`

:

```
print(sum(map(lambda x: a[x],b),()))
```

Output:

```
(0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1)
```

U9-Forward
posted this

## Have an answer?

JD

im confused, your code works.