hozan - 4 years ago 218

Python Question

I want to find the average of a 2-dimensional list in Python.

This is an example of my list:

`list = [[x, y, 1], [x, y, 1], [x, y, 2]]`

I want to add the third row and find the average of that row only.

This is my code:

`def averageList(list):`

averagefile = []

sum = 0

for count in range(0, len(list)):

try:

sum = sum + float(list[count][2])

except ValueError:

print ''

average = sum / len(list[count])

averagefile.append(average)

print averagefile

#return averageList

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
>>> L = [['x','y',1], ['x','y',1], ['x','y',2]]
>>> sum(entry[-1] for entry in L) / float(len(L))
1.3333333333333333
```

This is same as:

```
>>> sum(entry[2] for entry in L) / float(len(L))
```

since the last position is index `2`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**