Python: sorting dictionary of dictionaries

Python: sorting dictionary of dictionaries

To sort a dictionary of dictionaries in Python based on a specific key within the inner dictionaries, you can use the sorted function and a custom sorting key function. Here's how you can do it:

Suppose you have a dictionary of dictionaries like this:

data = { 'entry1': {'score': 85, 'age': 25}, 'entry2': {'score': 92, 'age': 30}, 'entry3': {'score': 78, 'age': 28}, # Add more entries as needed } 

And you want to sort it based on the 'score' key within each inner dictionary. Here's how you can do it:

sorted_data = dict(sorted(data.items(), key=lambda item: item[1]['score'], reverse=True)) 

In this code:

  • data.items() extracts the key-value pairs from the original dictionary.
  • The key parameter in the sorted function specifies a custom sorting key function. In this case, we use a lambda function that retrieves the 'score' key from the inner dictionary (i.e., item[1]['score']) to use for sorting.
  • We use reverse=True to sort in descending order (highest scores first).

The sorted_data variable will contain the sorted dictionary of dictionaries based on the 'score' key.

If you want to sort in ascending order (lowest scores first), simply remove reverse=True.

Examples

  1. "How to sort a dictionary of dictionaries by key in Python"

    • This query explores how to sort a dictionary of dictionaries by the dictionary's keys.
    # Dictionary of dictionaries data = { 'b': {'value': 2}, 'a': {'value': 1}, 'c': {'value': 3} } # Sorting by outer dictionary keys sorted_data = dict(sorted(data.items(), key=lambda x: x[0])) print("Sorted by keys:", sorted_data) 
  2. "Sorting a dictionary of dictionaries by inner key"

    • This query examines how to sort a dictionary of dictionaries by a specific key in the inner dictionaries.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Sorting by the 'name' key in inner dictionaries sorted_data = dict(sorted(data.items(), key=lambda x: x[1]['name'])) print("Sorted by inner 'name' key:", sorted_data) 
  3. "Sorting a dictionary of dictionaries by multiple inner keys in Python"

    • This query describes sorting a dictionary of dictionaries based on multiple inner keys.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Sorting by 'name' and then by 'age' in inner dictionaries sorted_data = dict(sorted(data.items(), key=lambda x: (x[1]['name'], x[1]['age']))) print("Sorted by 'name' and 'age':", sorted_data) 
  4. "Python: sorting a dictionary of dictionaries by outer and inner keys"

    • This query explores sorting a dictionary of dictionaries by outer keys, then by inner keys.
    # Dictionary of dictionaries data = { 'entry3': {'name': 'Charlie', 'age': 35}, 'entry1': {'name': 'Alice', 'age': 30}, 'entry2': {'name': 'Bob', 'age': 40} } # Sorting by outer dictionary keys and then by inner 'age' key sorted_data = dict(sorted(data.items(), key=lambda x: (x[0], x[1]['age']))) print("Sorted by outer keys and inner 'age':", sorted_data) 
  5. "Sorting a dictionary of dictionaries by nested keys"

    • This query focuses on sorting a dictionary of dictionaries by keys within nested dictionaries.
    # Dictionary of dictionaries with nested structures data = { 'entry3': {'person': {'name': 'Charlie', 'age': 35}}, 'entry1': {'person': {'name': 'Alice', 'age': 30}}, 'entry2': {'person': {'name': 'Bob', 'age': 40}} } # Sorting by 'name' key in the nested dictionaries sorted_data = dict(sorted(data.items(), key=lambda x: x[1]['person']['name'])) print("Sorted by nested 'name' key:", sorted_data) 
  6. "Python sorting dictionary of dictionaries by inner key with custom comparator"

    • This query discusses sorting a dictionary of dictionaries by inner keys using a custom comparator.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Custom comparator function to sort by 'age' sorted_data = dict(sorted(data.items(), key=lambda x: x[1]['age'])) print("Sorted by custom comparator (age):", sorted_data) 
  7. "Sorting a dictionary of dictionaries by inner key with reversed order"

    • This query explores sorting a dictionary of dictionaries by inner keys in descending order.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Sorting by 'age' in descending order sorted_data = dict(sorted(data.items(), key=lambda x: -x[1]['age'])) print("Sorted by 'age' (descending):", sorted_data) 
  8. "Python sorting dictionary of dictionaries by multiple criteria with reverse order"

    • This query discusses sorting by multiple criteria in a dictionary of dictionaries with reverse order.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Sorting by 'name' in ascending order and 'age' in descending order sorted_data = dict(sorted(data.items(), key=lambda x: (x[1]['name'], -x[1]['age']))) print("Sorted by 'name' (asc) and 'age' (desc):", sorted_data) 
  9. "Sorting a dictionary of dictionaries by specific value in Python"

    • This query explores how to sort a dictionary of dictionaries by a specific value.
    # Dictionary of dictionaries data = { 'entry1': {'name': 'Charlie', 'age': 35}, 'entry2': {'name': 'Alice', 'age': 30}, 'entry3': {'name': 'Bob', 'age': 40} } # Sorting by 'age' sorted_data = dict(sorted(data.items(), key=lambda x: x[1]['age'])) print("Sorted by 'age':", sorted_data) 

More Tags

windows-defender excel-2010 diagnostics qstackedwidget ksqldb ruby-on-rails-3.2 voyager autosuggest xamarin.forms.listview evaluate

More Python Questions

More Chemical reactions Calculators

More Geometry Calculators

More Entertainment Anecdotes Calculators

More Auto Calculators