Solution 1

``````def eldest_customer_per_state(customers_dict):
results = {}
for state, customers in customers_dict.items():
eldest_customer = None
for customer in customers:
if eldest_customer is None or customer['age'] > eldest_customer['age']:
eldest_customer = customer
results[state] = eldest_customer

return results``````

# Eldest customer per state

With the same structure as before, now you need to write a function that finds the eldest customer per state. Example:

``````customers = {
'UT': [{
'name': 'Mary',
'age': 28
}, {
'name': 'John',  # Eldest
'age': 31
}, {
'name': 'Robert',
'age': 16
}],
'NY': [{
'name': 'Linda',  # Eldest (only customer)
'age': 71
}],
'CA': [{
'name': 'Barbara',
'age': 15
}, {
'name': 'Paul',
'age': 18
}, {
'name': 'Helen',  # Eldest
'age': 29
}]
}
results = eldest_customer_per_state(customers)
print(results)
``````

Prints:

``````expected_result = {
'UT': {
'name': 'John',
'age': 31
},
'NY': {
'name': 'Linda',
'age': 71
},
'CA': {
'name': 'Helen',
'age': 29
}
}
``````

### Test Cases

test eldest customers with states -

``````def test_eldest_customers_with_states():
"""Should return the eldest customer per state."""
customers = {
'UT': [{
'name': 'Mary',
'age': 28
}, {
'name': 'John',
'age': 31
}, {
'name': 'Robert',
'age': 16
}],
'NY': [{
'name': 'Linda',
'age': 71
}, {
'name': 'Lisa',
'age': 25
}, {
'name': 'Daniel',
'age': 45
}],
'CA': [{
'name': 'Barbara',
'age': 15
}, {
'name': 'Paul',
'age': 18
}, {
'name': 'Helen',
'age': 29
}]
}
expected_result = {
'UT': {
'name': 'John',
'age': 31
},
'NY': {
'name': 'Linda',
'age': 71
},
'CA': {
'name': 'Helen',
'age': 29
}
}
assert eldest_customer_per_state(customers) == expected_result
``````

test eldest customers with empty states -

``````def test_eldest_customers_with_empty_states():
customers = {
'UT': [{
'name': 'Mary',
'age': 28
}, {
'name': 'John',
'age': 31
}],
'NY': []
}
expected_result = {
'UT': {
'name': 'John',
'age': 31
},
'NY': None
}
assert eldest_customer_per_state(customers) == expected_result
``````
def eldest_customer_per_state(customers_dict): pass