<jupyter_start><jupyter_text>Sorting a list of Dictionaries by a Common Key  Problem: Sort the entries in a list of dictionaries by one or more of the dictionary values. Solution: Using the `operator` module's `itemgetter` function, this sorting becomes easy.- Example:<jupyter_code>rows = [
    {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003},
    {'fname': 'David', 'lname': 'Beazley', 'uid': 1002},
    {'fname': 'John', 'lname': 'Cleese', 'uid': 1001},
    {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}
]<jupyter_output><empty_output><jupyter_text>It's easy to output these rows ordered by any of the fields common to all of the dictionaries.- Example:<jupyter_code>from operator import itemgetter

rows_by_fname = sorted (rows, key = itemgetter('fname'))
rows_by_uid = sorted (rows, key = itemgetter('uid'))

print(rows_by_fname)
print(rows_by_uid)<jupyter_output>[{'fname': 'Big', 'lname': 'Jones', 'uid': 1004}, {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}]
[{'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}]<jupyter_text>The `itemgetter()` function can also accept multiple keys.- Example:<jupyter_code>[{'fname': 'David', 'uid': 1002, 'lname': 'Beazley'},
 {'fname': 'John', 'uid': 1001, 'lname': 'Cleese'},
 {'fname': 'Big', 'uid': 1004, 'lname': 'Jones'},
 {'fname': 'Brian', 'uid': 1003, 'lname': 'Jones'}]
rows_by_lfname = sorted(rows, key = itemgetter('lname', 'fname'))
print(rows_by_lfname)<jupyter_output>[{'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, {'fname': 'Big', 'lname': 'Jones', 'uid': 1004}, {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}]<jupyter_text>The functionaly of the ` itemgetter()` is sometimes replaces by lambda expressions.- Example:<jupyter_code>rows_by_fname = sorted(rows, key=lambda r: r['fname'])
rows_by_lfname = sorted(rows, key=lambda r: (r['lname'],r['fname']))<jupyter_output><empty_output><jupyter_text>The solution involving `itemgetter()` runs a bit faster.Also, this technique can be applied to functions such as min() and max().- Example:<jupyter_code>min(rows, key = itemgetter('uid'))
max(rows, key = itemgetter('uid'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21619399917796958, 'fraction_numerical': 0.032881216605014384, 'mean_word_length': 7.250847457627119, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '112435', 'license': 'MIT', 'n_tokens_mistral': 988, 'n_tokens_neox': 891, 'n_words': 248, 'path': 'data structures & algorithms/13.ipynb', 'repo_name': 'Gth1205/py-cookbook', 'size': '2433'}