You have a list with duplicates: [1, 2, 2, 3, 3, 3]. How do you get just the unique values?
Loop through, only add to a new list if it's not already there?
Works. But Python has a built-in collection that does this for free: a set. Curly brackets, comma-separated, duplicates dropped automatically:
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # {1, 2, 3}Order is not preserved (sets are unordered). Items are unique by definition.
What if I have an existing list and want to dedupe it?
Wrap it in set():
list_with_dupes = [1, 2, 2, 3, 3, 3]
unique = set(list_with_dupes)
print(unique) # {1, 2, 3}Duplicates dropped in one call. Best tool for the job.
empty = set() # NOT {} — that's an empty dict
three = {1, 2, 3}
from_list = set([1, 2, 2, 3]) # {1, 2, 3}Watch out: {} is an empty dict, not an empty set. To create an empty set, write set().
s = {1, 2, 3}
s.add(4) # {1, 2, 3, 4}
s.add(2) # {1, 2, 3, 4} — already there, no error
s.remove(3) # {1, 2, 4}
s.discard(99) # {1, 2, 4} — silent if not present (vs remove which errors)inVery fast on sets, regardless of size. Slower on lists (linear scan).
2 in {1, 2, 3} # True
5 in {1, 2, 3} # Falsefor x in {3, 1, 2}:
print(x)
# could print in any order: 1 2 3, or 3 1 2, etc.If order matters, sort it: sorted({3, 1, 2}) → [1, 2, 3] (returns a list).
Sets have algebra-style operations:
a = {1, 2, 3}
b = {2, 3, 4}
a | b # {1, 2, 3, 4} — union
a & b # {2, 3} — intersection (both)
a - b # {1} — difference (in a, not in b)set(my_list) then list(set(my_list)) if you need a list back.in test: if you'll do many membership checks against the same collection."common items" = a & b; "only in a" = a - b.Create a free account to get started. Paid plans unlock all tracks.
You have a list with duplicates: [1, 2, 2, 3, 3, 3]. How do you get just the unique values?
Loop through, only add to a new list if it's not already there?
Works. But Python has a built-in collection that does this for free: a set. Curly brackets, comma-separated, duplicates dropped automatically:
numbers = {1, 2, 2, 3, 3, 3}
print(numbers) # {1, 2, 3}Order is not preserved (sets are unordered). Items are unique by definition.
What if I have an existing list and want to dedupe it?
Wrap it in set():
list_with_dupes = [1, 2, 2, 3, 3, 3]
unique = set(list_with_dupes)
print(unique) # {1, 2, 3}Duplicates dropped in one call. Best tool for the job.
empty = set() # NOT {} — that's an empty dict
three = {1, 2, 3}
from_list = set([1, 2, 2, 3]) # {1, 2, 3}Watch out: {} is an empty dict, not an empty set. To create an empty set, write set().
s = {1, 2, 3}
s.add(4) # {1, 2, 3, 4}
s.add(2) # {1, 2, 3, 4} — already there, no error
s.remove(3) # {1, 2, 4}
s.discard(99) # {1, 2, 4} — silent if not present (vs remove which errors)inVery fast on sets, regardless of size. Slower on lists (linear scan).
2 in {1, 2, 3} # True
5 in {1, 2, 3} # Falsefor x in {3, 1, 2}:
print(x)
# could print in any order: 1 2 3, or 3 1 2, etc.If order matters, sort it: sorted({3, 1, 2}) → [1, 2, 3] (returns a list).
Sets have algebra-style operations:
a = {1, 2, 3}
b = {2, 3, 4}
a | b # {1, 2, 3, 4} — union
a & b # {2, 3} — intersection (both)
a - b # {1} — difference (in a, not in b)set(my_list) then list(set(my_list)) if you need a list back.in test: if you'll do many membership checks against the same collection."common items" = a & b; "only in a" = a - b.