> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Iterating Lists

> This lesson explores using a for loop in Python to iterate through a list and calculate the average age from a dataset.

In this lesson, we will explore how to use a for loop to iterate through a list in Python to calculate the average age from a dataset. The process involves summing all the ages and then dividing that total by the number of ages using the built-in len() function.

## Step 1: Calculate the Total Sum of Ages

Start by initializing a variable called total to zero. Then, iterate over each element in the list to accumulate the sum of the ages:

```python theme={null}
ages = [56, 72, 24, 46]
total = 0
for age in ages:
    total += age
```

During each iteration, the value of the current age is added to the total. After the loop completes, the total sum of the ages becomes 198.

## Step 2: Compute the Average Age

After obtaining the total sum, calculate the average by dividing the total by the number of items in the list:

```python theme={null}
average = total / len(ages)
print(average)
# Output:
# 49.5
```

<Callout icon="lightbulb" color="#1CB2FE">
  In this example, the average age is computed by dividing the sum of ages (198) by the length of the list (4), which results in an average age of 49.5.
</Callout>

That’s all for this lesson. More exciting topics and tutorials will be covered soon—stay tuned for further updates on Python programming techniques!

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/python-basics/module/a115db9d-996c-4cfc-be27-9cfd7e6d77f5/lesson/0340fa6d-022a-4e45-a628-39b26b33a538" />
</CardGroup>
