Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does a set remove duplicate values but not keep my original order?

Quick answer

A set is designed to hold unique values, not to preserve the order of the sequence you started with. Python’s documentation describes sets as unordered, so their iteration or printed order may look unexpected [S1]. If original position matters, keep a list as your result. You can use a separate set to check which values you have already added, while the list records them in first-seen order.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I converted a list to a set to remove repeated values, but now the values appear in a different order. Did Python shuffle them?

Geez

Not necessarily. The important point is that a set does not promise to retain the order of the list you converted. Python describes sets as unordered collections of unique elements and warns that their iteration or printed output can appear in an unexpected order [S1]. A set is a good fit when you care about whether a value is present or want unique membership; it is not the right container for remembering the original positions.

For example, if your input is ['pear', 'apple', 'pear', 'plum'], converting it to a set can remove the repeated 'pear', but you should not rely on the resulting display order matching the input. The original list represents the sequence you received; the set represents unique membership.

Follow-up question

I need to remove repeats but keep the first appearance of each value. What should I use?

Geez

Keep a list for the output and use a set only to check whether each value has appeared before. Walk through the input from left to right: add a value to the result list the first time you see it, and skip later occurrences. That way, the list records the first-seen order while the set handles the membership check.

Copyable example:

values = ['pear', 'apple', 'pear', 'plum', 'apple']
seen = set()
unique_in_order = []

for value in values:
    if value not in seen:
        unique_in_order.append(value)
        seen.add(value)

print(unique_in_order)
# ['pear', 'apple', 'plum']

Here, the list is the ordered result. The set is not used as the output; it is just a record of values already encountered. This example assumes the values are suitable for set membership, as ordinary strings are.

Follow-up question

Could I use sorted(set(values)) instead? I want a predictable result.

Geez

You can use sorted(set(values)) when you want unique values in sorted order. But sorting creates a different order; it does not restore the order in which values first appeared in your input [S1]. For the example above, sorting would put the unique strings in alphabetical order rather than leaving 'pear' first.

Choose based on what “predictable” means for your task:

  1. Keep first-seen order: build a result list while checking a separate set, as in the example.
  2. Use sorted order: sort the unique values if alphabetical or other sorted output is what you want.
  3. Use a set alone: do that when uniqueness or membership matters and sequence order does not.

Follow-up question

How can I tell which approach fits my code before I change it?

Geez

Look at what the next part of your program expects. If it displays values in the same sequence they arrived, compares positions, or processes the first item before later items, preserve an ordered list. If it only asks whether a value exists or needs a collection of unique members without relying on their sequence, a set may fit. If the required output is sorted, sort the unique values explicitly.

A practical check is to write down the intended output for an input containing a repeat that appears after other values. For ['pear', 'apple', 'pear', 'plum'], first-seen deduplication should give ['pear', 'apple', 'plum']. If that is your desired result, retain the list-based approach and use the set only for membership checks.

A practical checklist

  • Decide whether the result needs first-seen order, sorted order, or only unique membership.

  • Write down the expected output for an example containing a repeated value.

  • Keep a list as the result when original sequence order matters.

  • Use a set separately to track values already encountered.

  • Use sorted(set(values)) only when sorted unique output—not original order—is intended.

A few more useful answers

Does a set always print values in a different order from the input?

No. Its displayed order may sometimes look familiar, but a set is unordered, so that appearance is not a promise you should rely on [S1].

Will sorting unique values preserve their original positions?

No. Sorting gives a sorted order, not the input’s first-seen order [S1]. Use a list-based result if first appearance matters.

Why keep both a list and a set?

They have different jobs in this pattern: the list records the order you want to return, while the set records which values have already appeared.

Sources

References used for this guide.

  1. [S1] Data Structures — Sets and ordering
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work