syntax Copy
s = {val1, val2, ...}
s = set(iterable)example Copy
tags = {"python" , "tutorial" , "beginner" }
from_list = set([1 , 2 , 2 , 3 , 3 , 3 ])
print (from_list)
empty_set = set()
print (type (empty_set))output
{1, 2, 3}
<class 'set'>Note Use set() for an empty set, NOT {}. Empty braces {} create an empty dictionary, not a set.
Set Comprehensions PY · Tuples & Sets syntax Copy
{expression for item in iterable if condition}example Copy
emails = ["[email protected] " , "[email protected] " , "[email protected] " , "[email protected] " ]
unique_lower = {e.lower() for e in emails}
print (unique_lower)output
{'[email protected] ', '[email protected] ', '[email protected] '}Note Set comprehensions automatically deduplicate results. Useful for extracting unique transformed values from a collection.