Skip to content
techsnow

Data Structures

Sets

A set stores unique values with no order. It is great for removing duplicates and for fast membership checks.

tags = {"python", "code", "python"}
print(tags)                   # {'python', 'code'}

tags.add("ai")
tags.discard("code")

a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)   # union        {1, 2, 3, 4, 5}
print(a & b)   # intersection {3}
print(a - b)   # difference   {1, 2}

unique = list(set([1, 1, 2, 3, 3]))  # remove duplicates