• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Python | Sets
A set is a collection of unordered unique, immutable (hashable) items.
Sets are mutable; which means you can add or remove items.
Items in the set need to be of hashable types (integers, strings, tuples with immutable items).
Mixed hashable types can be added to the same set.
Duplicate items are automatically removed.

Creating basic sets:
Use curly braces ({}) to create a set.
Use commas to separate items in the set.
x = {0, 1, 2, 3, 2}
print(type(x)) # Output: <class 'set'>

# sets are unordered so the order of the output may vary
print(x) # Output: {0, 1, 2, 3}
Sets with one item:
y = {0}
print(y) # Output: {0}
Empty sets:
empty_set = set()
print(type(empty_set)) # Output: <class 'set'>
print(empty_set) # Output: set()
Important: empty curly braces ({}) create an empty dictionary, not a set:
empty_dictionary = {}
print(type(empty_dictionary)) # Output: <class 'dict'>
print(empty_dictionary) # Output: {}
Hashable elements:
You get a TypeError if you use an unhashable element to declare a set:
# Raises TypeError: unhashable type: 'set'
#z = {{1, 2}, {3, 4}}
Using tuples, strings, numbers, immutable sets should work fine:
z = {(1,2), (3,), 'a', "hello", 1, 1.5, frozenset([1, 2])}
print(z) # Output: {1, 1.5, (1, 2), frozenset({1, 2}), 'a', (3,), 'hello'}
Creating sets from lists:
set_from_list = set([1, 2, 3, 2])
print(set_from_list) # Output: {1, 2, 3} - duplicates are removed automatically
Creating sets from strings:
set_from_string = set("python")
print(set_from_string) # Output: {'p', 'o', 'y', 't', 'h', 'n'} - reminder: the set is unordered
Creating sets from tuples:
set_from_tuple = set((1, 2, 3, 2))
print(set_from_tuple) # Output: {1, 2, 3} - duplicates are removed automatically
Set comprehensions:
# set of squares of even numbers
dynamic_set = {x**2 for x in range(5) if x % 2 ==0}
print(dynamic_set) # Output: {0, 4, 16}
Set access and iteration:
Sets do not support indexing because they are unordered:
print(x[1]) # Raises TypeError: 'set' object is not subscriptable
To access items in a set, iterate over it using a loop or convert it to a list:
for i in x:
  print(i)

w = list(x)
print(w[0])
Common set operations:
  • Adding elements:
    x = {1}
    
    # Add single element
    x.add(2)
    print(x) # Output: {1, 2}
    
    # Add multiple elements
    x.update([3, 4])
    print(x) # Output: {1, 2, 3, 4}
  • Removing elements:
    x = {'a', 'b', 'c'}
    
    # remove an element
    x.remove('a')
    print(x) # Output: {'b', 'c'}
    
    # you get a KeyError if the element doesn't exist
    #x.remove('d') # Raises KeyError: 'd'
    
    # Use discard if not sure if the element exist
    x.discard('d') # OK
    
    # Pop arbitrary element
    element = x.pop()
    print(f"element: {element} - remaining: {x}")
  • Union: x | y (elements in x or y)
    print({1, 2, 3} | {4, 5, 2}) # Output: {1, 2, 3, 4, 5}
  • Intersection: x & y (elements in x and y)
    print({1, 2, 3} & {4, 5, 2}) # Output: {2}
  • Difference: x - y (elements in x but not in y)
    print({1, 2, 3} - {4, 5, 2}) # Output: {1, 3}
  • Symmetric difference: x ^ y (elements in either x or y, but not both)
    print({1, 2, 3} ^ {4, 5, 2}) # Output: {1, 3, 4, 5}
  • Membership: i in x (check if i in x)
    print(1 in {1, 2, 3}) # Output: True
    print(1 not in {1, 2, 3}) # Output: False
    print(4 in {1, 2, 3}) # Output: False
    print(4 not in {1, 2, 3}) # Output: True
© 2025  mtitek