Code Logo

Guest List Cleanup with dict.fromkeys()

Published at22 Apr 2026
Python List and Dictionary Easy 0 views
Like0

An event organizer exports a guest list from several small registration forms and notices that some names appear more than once. The organizer does not want repeated names in the final printed list, but the first appearance of each guest should stay in its original position.

Your task is to take a list of guest names and return a new list where duplicates have been removed. If a name appears again later, that repeated copy should be ignored because the guest is already in the cleaned list.

For example, if the input is ["Nina", "Rafi", "Nina", "Budi", "Rafi"], the result should be ["Nina", "Rafi", "Budi"]. The answer is not sorted alphabetically. It follows the first time each name appeared in the original list.

This is a useful beginner exercise because it mixes list order with dictionary behavior. You want list-style output, but a dictionary can help you recognize whether a name has already been seen before.

Example Input & Output

Example 1
Input
guests = ["Nina", "Rafi", "Nina", "Budi", "Rafi"]
Output
["Nina", "Rafi", "Budi"]
Explanation

Repeated names are removed, but the first appearance order stays the same.

Example 2
Input
guests = []
Output
[]
Explanation

An empty guest list stays empty.

Example 3
Input
guests = ["Lina", "Lina", "Lina"]
Output
["Lina"]
Explanation

Several copies of the same name collapse into one kept entry.

Algorithm Flow

Recommendation Algorithm Flow for Guest List Cleanup with dict.fromkeys()
Recommendation Algorithm Flow for Guest List Cleanup with dict.fromkeys()

Solution Approach

The tricky part of this problem is not removing duplicates by itself. The tricky part is removing duplicates while still preserving the first-seen order. A plain set would tell you which names are unique, but it would not be the nicest teaching tool here because this challenge is meant to highlight Python list-and-dictionary thinking.

One handy Python shortcut is dict.fromkeys(). When you pass a list of names into it, Python creates a dictionary whose keys are those names. If a name appears more than once, only one key is kept. Because modern Python dictionaries preserve insertion order, the first appearance stays in place.

That leads to a very compact answer:

return list(dict.fromkeys(guests))

The inner dictionary step removes the repeated names, and the outer list() call turns the remaining keys back into the list format required by the problem. It is short, but the behavior lines up perfectly with what the organizer needs.

You can also solve the same task more manually by keeping a dictionary of seen names and appending only the first time a name appears:

seen = {}
cleaned = []
for guest in guests:
    if guest not in seen:
        seen[guest] = True
        cleaned.append(guest)
return cleaned

That longer version makes the rule more visible, while dict.fromkeys() shows the Python-specific shortcut. Both are correct. The important lesson is that dictionaries are not only for storing final answers. They are also very helpful for tracking which values have already been encountered while you build a list.

The runtime is O(n) because each guest name is processed once, and the extra space depends on how many distinct names remain in the cleaned result. For an easy problem, this is a nice example of how Python collections can work together naturally.

Best Answers

python - Approach 1
def clean_guest_list(guests):
    seen = {}
    cleaned = []
    for guest in guests:
        if guest not in seen:
            seen[guest] = True
            cleaned.append(guest)
    return cleaned