Skip to content

Set 集合 {}

Properties of Set:

  • It doesn't support repeated data.
  • It's unordering so it don't have index.
  • So, it's suitable to remove repeated data.
  • It's unordering feature will cause the set item ordering changes.
  • It also allow to make changes to the data.

Syntax

Define Set

py
set_1 = {element1, element2, elementN}

# Empty set
set_empty_1 = set() # <class 'set'>
set_empty_2 = {} # printed type = <class 'dict'>

Different of define set compare with other data container

Common Usage

set.py

Define a set

py
# Define a set
fruits_set = {"Grape", "Kiwi", "Banana", "Apple", "Mango", "Watermelon"}
print(fruits_set)
print(type(fruits_set))

Output:

{'Mango', 'Grape', 'Banana', 'Kiwi', 'Watermelon', 'Apple'}
<class 'set'>

Add an element

py
# Add an new element
fruits_set.add("Orange")
print(fruits_set)

# Try add repeated data
fruits_set.add("Grape")
print(fruits_set) # Grape has repeated, python will remove if same.

Output:

{'Grape', 'Orange', 'Apple', 'Watermelon', 'Mango', 'Banana', 'Kiwi'}
{'Grape', 'Orange', 'Apple', 'Watermelon', 'Mango', 'Banana', 'Kiwi'}

Remove an element

py
# Remove an element
fruits_set.remove("Apple")
print(fruits_set)

Output:

{'Banana', 'Kiwi', 'Grape', 'Watermelon', 'Orange', 'Mango'}

Randomly get any one if the element from the set

py
# Get an random item from the set
# set.pop() means remove item, since it dont have index, random item will be removed
random_item = fruits_set.pop()
print(f"{fruits_set.pop()} has been removed.")
print(fruits_set)

Output: (1st trial)

Mango has been removed.
{'Orange', 'Kiwi', 'Banana', 'Grape'}

Output: (2nd trial)

Banana has been removed.
{'Kiwi', 'Mango', 'Grape', 'Watermelon'}

Clear the set

py
# Clear entire set
fruits_set.clear()
print(fruits_set)

Output:

set()

Info

set() means empty set.

Combine a Set (Union)

py
# Combine / union a set
set_a = {1, 2, 3}
set_b = {1, 4, 2}
set_c = set_a.union(set_b)
print(set_c) # Combine + remove repeated

Results:

{1, 2, 3, 4}

Get length of the set

py
# Get length of the set
set_alpha = {5, 5, 0, 6, 7, 8}
print(len(set_alpha)) # Still remove repeated data, 5

Output:

5

Iterating over a set

  • Since set doesnt have index, so we can't use while loop but we can use for loop.
py
set_for_iterating = {"John", "Delton", "Alison", "Ed"}

for i in set_for_iterating:
    print(i)

original_set = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
plus_1_set = set()

for item in original_set:
    item += 1
    plus_1_set.add(item)

print(plus_1_set)

Output:

Delton
Ed
Alison
John
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Usage - Difference

Find the difference between 2 set and form a new set

py
new_set_1 = {1, 2, 3}
new_set_2 = {2, 4, 6}

# The same element on this 2 sets: 2
# Different element on this sets 1 and set 2:
# Set1 = 1, 3
# Set2 = 4, 6

# Get the difference between 2 sets
different_set_btwn_1_2 = new_set_1.difference(new_set_2)
print(different_set_btwn_1_2) # {1, 3} only exists on set_1

different_set_btwn_2_1 = new_set_2.difference(new_set_1)
print(different_set_btwn_2_1) # {4, 6} only exists on set_2

Output:

{1, 3}
{4, 6}

Find the difference between 2 set and update the set

py
set_3 = {0, 1, 2}
set_4 = {1, 3, 4}

# Repeated data = 1
# find the difference between set 2 and set 1, and remove the repeated and
# return a new set without the repeated element
set_3.difference_update(set_4)
print(set_3)

Output:

{0, 2}

Conclusion

Set conclusion