Python - Struggling to Sort a List of Dictionaries by Nested Integer Values
I'm working with a scenario while trying to sort a list of dictionaries based on a nested integer value. I have a list of dictionaries that represent employees, and I need to sort them by their age, which is nested under a 'profile' key. The scenario arises because I keep getting a `TypeError: '<' not supported between instances of 'NoneType' and 'int'` when there are missing age values. Here's what my list looks like: ```python employees = [ {'name': 'Alice', 'profile': {'age': 30}}, {'name': 'Bob', 'profile': {'age': None}}, {'name': 'Charlie', 'profile': {'age': 25}}, {'name': 'David', 'profile': {'age': 35}}, {'name': 'Eve', 'profile': {'age': None}} ] ``` I've tried using a lambda function with the `sorted()` method: ```python sorted_employees = sorted(employees, key=lambda x: x['profile']['age']) ``` However, this doesn't handle the cases where age is `None`, and it throws the TypeError. I attempted to provide a default value in the sorting function like this: ```python sorted_employees = sorted(employees, key=lambda x: x['profile']['age'] if x['profile']['age'] is not None else float('inf')) ``` This solution works, but it places employees with `None` age at the end, which is not exactly what I want. I need to sort them in such a way that if age is missing, those entries should be treated as the youngest employees (or at least come before those with a defined age). Is there a way to achieve this while maintaining the order of employees with defined ages? Any help would be greatly appreciated! My development environment is macOS. Any ideas what could be causing this?