datarekha
Python Easy Asked at AmazonAsked at Airbnb

How do you sort a list of dictionaries by a specific key in Python, and what is the difference between sorted() and list.sort()?

The short answer

Use sorted() with a key= lambda to produce a new sorted list, or list.sort() to sort in place. Both use Timsort and run in O(n log n). sorted() works on any iterable and returns a new list; list.sort() operates in place and returns None.

How to think about it

The interviewer is checking that you know Python’s sorting API well enough to dodge its classic bug — list.sort() returns None — and that you’ll reach for operator.itemgetter rather than always defaulting to a lambda.

The mental model is the simplest part: key= is a transform. Python calls it once per element, sorts on the transformed values, and reorders the originals to match — you never write comparison logic yourself.

from operator import itemgetter

employees = [
    {"name": "Alice", "salary": 95000},
    {"name": "Bob",   "salary": 82000},
    {"name": "Carol", "salary": 110000},
]

by_salary = sorted(employees, key=itemgetter("salary"))  # NEW list, original intact
employees.sort(key=itemgetter("salary"))                 # in place, returns None

itemgetter is a touch faster than a lambda because it’s implemented in C — no per-element Python call. For plain key access, prefer it.

A worked example

The interesting cases are multi-key sorts and Python’s stable ordering — negate a numeric field to flip just that one key to descending:

from operator import itemgetter

employees = [
    {"name": "Alice", "dept": "Eng",    "salary": 95000},
    {"name": "Bob",   "dept": "Design", "salary": 82000},
    {"name": "Carol", "dept": "Eng",    "salary": 110000},
    {"name": "Dan",   "dept": "Design", "salary": 91000},
]

print("By salary (asc):")
for e in sorted(employees, key=itemgetter("salary")):
    print(" ", e["name"], e["salary"])

# Tuple key: dept ascending, then salary descending (negate to flip)
print("By dept then salary desc:")
for e in sorted(employees, key=lambda e: (e["dept"], -e["salary"])):
    print(" ", e["dept"], e["name"], e["salary"])

# Stable: ties on dept keep their original relative order
print("Just by dept (stable):")
for e in sorted(employees, key=itemgetter("dept")):
    print(" ", e["dept"], e["name"])
By salary (asc):
  Bob 82000
  Dan 91000
  Alice 95000
  Carol 110000
By dept then salary desc:
  Design Dan 91000
  Design Bob 82000
  Eng Carol 110000
  Eng Alice 95000
Just by dept (stable):
  Design Bob
  Design Dan
  Eng Alice
  Eng Carol

Two ideas to lift out. A tuple key sorts by each element in turn — (dept, -salary) groups by department, then orders salaries high-to-low inside each group, the - flipping just that field. And the last block shows stability: Bob and Dan are both Design, and they keep their original order (Bob first, as in the input). That stability is what lets you build a multi-column sort by sorting repeatedly, least-significant key first.

sorted() vs .sort()

Both use Timsort — O(n log n) worst case, O(n) on already-sorted data. The only difference is whether the original survives: sorted() returns a new list and works on any iterable; .sort() mutates a list in place. When in doubt, use sorted() — it’s the safer default.

Learn it properly Lists

Keep practising

All Python questions

Explore further

Skip to content