Користувальницькькі налаштування

Налаштування сайту


programming:python:find_max_dictionary

How to find dictionary element with maximum field value

Given list of dictionaries I need to find one element of list (dictionary) with maximum field value:

people = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30},
    {'name': 'Charlie', 'age': 22}
]
 
# Find the dictionary with the maximum 'age'
oldest_person = max(people, key=lambda person: person['age'])
 
print(oldest_person)

Output:

{'name': 'Bob', 'age': 30}
programming/python/find_max_dictionary.txt · Востаннє змінено: 2024/10/24 12:50 повз charon