Sets
# Sets can store multiple values in one variable
# Sets can't be ordered and don't allow duplicate values
# Sets can't use indexes
# Define a set
set1 = {"a", "b", "c"}
set2 = set(("b", 1, False))
# Add new items to set
set1.add("d")
# Combine or update sets, you can use both sets and lists
set3.update(set4)
set4.update(list1)
# Create new set with a union of sets
set6 = set4.union(set5)
# Remove value from set - if it isn't there you will get an error
set4.remove(4)
# Discard will remove the item but without an error if value isn't there
set4.discard(4)
# Only use pop when you don't care about the order of the data
set1.pop()
Last updated