You use list(set(tags)) to remove duplicate tags before displaying them. The duplicates disappear, but so does the order the user chose. The list seems shuffled because a set does not preserve input order.
Use the same six tags to test deduplication and order preservation separately.
Why does list(set()) not preserve the original order?
tags = ["db", "api", "web", "db", "linux", "api"]
unique = list(set(tags))
print(unique) # Four values; their order is not guaranteed.set(tags) keeps one of each value but does not record where each first appeared. Turning the set back into a list cannot restore information that is gone. Even if db happens to appear first on your computer, do not rely on that result.
The Python documentation defines sets as unordered collections of distinct values. Removing duplicates is different from sorting the result.
How can you remove duplicates in first-seen order?
dict.fromkeys(tags) inserts each value once as a key. A repeated key does not move its existing position. Since Python 3.7, dictionaries preserve insertion order, so converting the keys to a list keeps first-occurrence order.
The diagram reduces the longer list to four representative entries: db, api, db, web. A set removes duplicates without guaranteeing input order; keys produced by dict.fromkeys retain the first-seen order db, api, web.
tags = ["db", "api", "web", "db", "linux", "api"]
unique = list(dict.fromkeys(tags))
print(unique) # ['db', 'api', 'web', 'linux']
print(tags) # The original list is unchanged.Check both properties: do db and api each appear only once, and do the remaining values follow their first input positions? This example satisfies both.
What if you need sorted(set()) instead?
If alphabetical order, rather than input order, is the requirement, sort explicitly.
tags = ["db", "api", "web", "db", "linux", "api"]
print(sorted(set(tags))) # ['api', 'db', 'linux', 'web']sorted() guarantees this order; set() alone does not. Decide whether the UI should retain a user's choices or show an alphabetical list before selecting the code.
What if a list or dictionary item raises TypeError?
Both set() and dict.fromkeys() require hashable items. A list or dictionary used as an item raises TypeError: unhashable type. Instead of inserting the whole object, choose a stable key for detecting duplicates. For a list of objects, decide whether to keep the first object with each ID or collapse objects with identical content.
Key takeaways
list(set(tags)) removes duplicates but does not guarantee input order. Use list(dict.fromkeys(tags)) to keep first-occurrence order, or sorted(set(tags)) when sorting is the goal. Deduplication and ordering are separate requirements.

