# Stuck on a problem

I’m trying to access data from a nested list and it is not working. I’m positive that my logic is wrong, but I don’t understand how. I’ll post the code below:

``````#Find the average of the birds counted
#Loop through list and parse out int
#Add integers together and count of each interger
#Divide integer sum by count

data_sites_and_birds_counted = [['A1', 28], ['A2', 32], ['A3', 1], ['A4', 0],
['A5', 10], ['A6', 22], ['A7', 30], ['A8', 19],
['B1', 145], ['B2', 27], ['B3', 36], ['B4', 25],
['B5', 9], ['B6', 38], ['B7', 21], ['B8', 12],
['C1', 122], ['C2', 87], ['C3', 36], ['C4', 3],
['D1', 0], ['D2', 5], ['D3', 55], ['D4', 62],
['D5', 98], ['D6', 32]]
birds = []
bird_count = 0
count = 0
for i in data_sites_and_birds_counted:

if type(i) == int:
bird_count = bird_count + data_sites_and_birds_counted[i]
count = count + 1
total = int(bird_count)/count
print(total)
``````

You’re looping through a list of lists. So `i` will always be a list, not an integer. The integers are nested.

yeah as said above, `i `is a list each time, so you want `i[1]`, which is the number part of each internal list.

I think inside your for loop you can just do:
`bird_count += i[1]`

you could probably replace the count thing with the length of the main data_sites_and_birds list. think in python its len() but don’t quote me i’m here to learn too!
good luck!

One way of doing it

``````data_sites_and_birds_counted = [['A1', 28], ['A2', 32], ['A3', 1], ['A4', 0],
['A5', 10], ['A6', 22], ['A7', 30], ['A8', 19],
['B1', 145], ['B2', 27], ['B3', 36], ['B4', 25],
['B5', 9], ['B6', 38], ['B7', 21], ['B8', 12],
['C1', 122], ['C2', 87], ['C3', 36], ['C4', 3],
['D1', 0], ['D2', 5], ['D3', 55], ['D4', 62],
['D5', 98], ['D6', 32]]

# Set counters to 0
counter = 0
bird_count = 0

for data in data_sites_and_birds_counted: # Do the first loop to get list
for count in data: # Do the second loop to get list values
if type(count) == int: # Check value type / Only need the integers
counter += 1 # Increase the counter by 1 for each loop
bird_count += count # Increase the bird counter by bird count in each list

# Print out the results
print(f'Count: {counter}')
print(f'Bird Count: {bird_count}')
print(f'Total: {round(bird_count/counter)}')
``````

Output:
Count: 26
Bird Count: 955
Total: 37

Just to throw out one more way

``````# Do the import
from itertools import chain

# List comprehention using itertools chain to get integers
num = [data for data in chain(*data_sites_and_birds_counted) if type(data) == int]

# Get list len
counter = len(data_sites_and_birds_counted)

# Get bird count
bird_count = sum(num)

# Get average
avg = round(bird_count/counter)

# Print results
print(f'Counter: {counter}')
print(f'Bird Count: {bird_count}')
print(f'Avg: {avg}')
``````

Thank you. I’ll study these. Question: you’re using print(f’…) I’m assuming this is `printf` , which is syntax from C and was used in earlier forms of Python but in Python3 is starting to be phased out. Is this understanding correct? If not, why do you use` f` as an argument in the print function?