python add set to set code example

Example 1: add list to set python

set.update() or |=

>>> a = set('abc')
>>> l = ['d', 'e']
>>> a.update(l)
>>> a
{'e', 'b', 'c', 'd', 'a'}

>>> l = ['f', 'g']
>>> a |= set(l)
>>> a
{'e', 'b', 'f', 'c', 'd', 'g', 'a'}

Example 2: add element in set python

thisset = {"apple", "banana", "cherry"}

thisset.add("orange")

print(thisset)

Example 3: get length of set python

# Create a set
seta = {5,10,3,15,2,20}
# Or
setb = set([5, 10, 3, 15, 2, 20])

# Find the length use len()
print(len(setb))