Technical skill practice
How to Sort a List in Python: key, Stable Ties and In-Place sort
Use sorted(items, key=...) to create a new sorted list in Python. Use items.sort(key=...) to reorder an existing list in place; sort returns None. A tuple key can express a primary and secondary ordering, while equal keys preserve their original relative order.
Start with a small, explicit task
Order fictional tasks by ascending priority number and then by task name. Compare that with sorting only by priority, where the two priority-two tasks should keep their original order. Check that creating a sorted result does not reorder the source list.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_sort_list.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
['label', 'archive', 'seal']
['label', 'seal', 'archive']
None
tasks = [
{"task": "seal", "priority": 2},
{"task": "label", "priority": 1},
{"task": "archive", "priority": 2},
]
ordered = sorted(tasks, key=lambda item: (item["priority"], item["task"]))
assert [item["task"] for item in ordered] == ["label", "archive", "seal"]
assert [item["task"] for item in tasks] == ["seal", "label", "archive"]
returned = tasks.sort(key=lambda item: item["priority"])
assert returned is None
assert [item["task"] for item in tasks] == ["label", "seal", "archive"]
assert sorted([]) == []
print([item["task"] for item in ordered])
print([item["task"] for item in tasks])
print(returned)
Explain the result
The tuple comparison first uses priority and then the task name when priorities tie. label comes first at priority one; archive precedes seal alphabetically at priority two. The first source-order assertion confirms that sorted returned a new outer list instead of reordering tasks.
The in-place sort uses only priority. seal and archive now have equal keys, so their original relative order is retained. That stable behavior is different from an explicit alphabetical tie-break. Both outer lists still reference the same task dictionaries; sorting does not create deeply independent record copies.
Avoid these mistakes
Do not assign tasks = tasks.sort(), which replaces the name with None after sorting the former list. Make the direction of each key explicit: reverse=True reverses the overall order, so mixed ascending/descending requirements need a carefully designed key or multiple deliberate stable passes.
Questions, answered
Should I use sort or sorted?
Use sorted when you need a new ordered outer list; use sort when changing the existing list is intended.
What happens when keys tie?
The priority-only example retains seal before archive because their equal-key relative order is stable.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.