- unordered**(unindexed)
- unchangeable(possible to remove & add)
- do not allow duplicate values
thisset = {"apple", "banana", "cherry"}
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4 = {"abc", 34, True, 40, "male"}
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset) # {'cherry', 'banana', 'apple'}
Constructor: set(())
thisset = set(("apple", "banana", "cherry"))
print(thisset) # {'cherry', 'banana', 'apple'}
Access Set Items
for x in thisset:
print(x)
print("banana" in thisset)
Add Set Items
thisset = {"apple", "banana", "cherry"}
# add()
thisset.add("orange")
print(thisset) # {'cherry', 'banana', 'orange', 'apple'}
# Add Sets: update()
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
# {'papaya', 'pineapple', 'mango', 'cherry', 'banana', 'apple'}
# add any iterable
mylist = ["kiwi", "orange"]
thisset.update(mylist)
Join Sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
set1.update(set2)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
x.symmetric_difference_update(y)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
Remove Set Items: remove(), discard()
thisset.remove("banana")
thisset.discard("banana")
x = thisset.pop()
clear()
thisset.clear()
del thisset
Loop Sets
for x in thisset:
print(x)