← All coding practice

Coding Practice · Beginner · October 2, 2026 · 3 min read

Python Coding Exercise #02: Sorting Names

Part of the series Python Practice Exercise — part 2

Before you start: Familiarity with print(), for loop, and built-in list methods in Python

The question

Seitsemän veljestä (“Seven Brothers”) is one of the first novels ever written in Finnish. It is the story of seven orphaned brothers learning to make their way in the world (read more on Wikipedia).

This program is supposed to print out the names of the brothers in alphabetical order, but it’s not working quite right yet. Please fix the program so that the names are printed in the correct order.

print("Simeoni")
print("Juhani")
print("Eero")
print("Lauri")
print("Aapo")
print("Tuomas")
print("Timo")

My Solution

names = ["Simeoni", "Juhani", "Eero", "Lauri", "Aapo", "Tuomas", "Timo"]
names.sort()
for name in names:
    print(names)

Output:

Aapo
Eero
Juhani
Lauri
Simeoni
Timo
Tuomas

How My Solution Works

1. Put the names in a list

names = ["Simeoni", "Juhani", "Eero", "Lauri", "Aapo", "Tuomas", "Timo"]

First, we store the names of the seven brothers in a variable called names. Second, we use square brackets [ ] to create a list.

Each name is wrapped in quotation marks " " because names are strings (text).

2. Sort the list with .sort()

names.sort()

.sort() is a built-in list method in Python. It rearranges the items in the list into order, and for strings, that means alphabetical order (A to Z).

Now the list changes. After this line, names holds:

["Aapo", "Eero", "Juhani", "Lauri", "Simeoni", "Timo", "Tuomas"]

The sorting happens once, on this single line, before the loop starts.

3. Print each name with a for loop

for name in names:
    print(name)

Now the list is sorted, but we still need to print it. A for loop goes through the names in the list one by one:

  1. On the first run, the loop variable name holds "Aapo", so print(name) prints Aapo.
  2. On the second run, name holds "Eero", and so on.
  3. The loop runs 7 times, once for each name, and then stops.

The loop simply goes through the items in a list. Here, the loop variable is name.

One thing to notice: the loop prints name, the loop variable that holds one brother’s name at a time. It does not print names, which is the whole list. If you wrote print(names) instead, you would get the entire list on one line.

Why not just reorder the print() lines?

You could fix the original program by simply rewriting the seven print() lines in alphabetical order, and for seven names that is perfectly fine.

But with a list and .sort(), Python does the ordering for you. So, you don’t have to manually reorder all seven names.

Just these three lines would work for 7 names or 7,000. It is also a more efficient way to write the program.

Key takeaways

  • A list stores several values in one variable, written with square brackets: ["a", "b", "c"]. Lists are explained in more detail in Core Data Structures in Python.
  • .sort() puts the items of a list in order. Strings are sorted alphabetically.
  • .sort() changes the list itself and returns nothing, so print(names.sort()) prints None, not the sorted list. Call .sort() on its own line, then print the list.
  • A for loop visits each item in a list one by one, and the loop variable holds the current item.
  • Choose clear variable names: plural for the list, singular for one item.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · lists · sorting · loops · strings