Skip to content
techskilldaily.
The archiveNo. 026
A little Python

Sort a copy. Keep the original.

Use sorted() when you want an ordered view without rearranging your source list.

2 min read 5 min to try
MAKE ROOM FORoneSMALL SKILL

The original order may tell a story.

A list can reflect the order things arrived. You might want to display those values alphabetically while keeping that arrival order available. Python’s sorted() returns a new list, leaving the original list in place.

The example stores the result under ordered. Printing both lists makes the difference visible. This is a useful habit whenever a transformation might erase information you still care about.

Two useful views

Arrival order

pear → apple → banana

Alphabetical copy

apple → banana → pear

sorted() returns a new list; arrival remains unchanged.

Choose the operation that matches your intention.

The list method .sort() changes a list in place. sorted() creates a new sorted list. Neither is universally better: the important question is whether other parts of your program still need the starting order.

Keep this exercise to simple lowercase words. Sorting mixed uppercase and lowercase text, accented names, or values of different types introduces extra choices. First make the difference between “new view” and “change the source” clear with a small predictable example.

A small example to try
arrival = ["pear", "apple", "banana"]
ordered = sorted(arrival)
print(ordered)
print(arrival)
Your turn 5 minutes

Make it yours.

  1. Run the example and compare its two lists.
  2. Add "apricot" at the end of arrival and rerun.
  3. Check that it appears alphabetically in ordered but last in arrival.

What changed when you tried it?

A little nudge

Store the return value from sorted() in a variable.

What to look for

The first list is ['apple', 'banana', 'pear']; the second remains ['pear', 'apple', 'banana']. Adding apricot changes both results while preserving arrival order in the original.

Take it one small step further

Try sorted(arrival, reverse=True) for a reversed alphabetical view.

Keep this bit

Preserve the source when its order still means something.

Read more: Python · Sorting techniques ↗

No submission needed. This one is just for you.