Create and Add Elements to a Set
In this first step, you will learn how to create sets and add new elements to them. Sets are collections of unique items, meaning they automatically discard any duplicates.
Your environment includes an empty file named set_basics.py. Using the file explorer on the left side of the editor, find and open ~/project/set_basics.py.
Add the following Python code to the file. This code demonstrates several ways to create a set.
## Method 1: Using curly braces {}
## This creates a set with initial elements.
my_set = {'apple', 'banana', 'cherry'}
print("Set created with braces:", my_set)
print("Type of my_set:", type(my_set))
## Note: Sets automatically remove duplicate elements.
duplicate_set = {'apple', 'banana', 'apple'}
print("Set with duplicates:", duplicate_set)
## Method 2: Using the set() constructor on an iterable (like a string)
## This creates a set from the unique characters in the string.
char_set = set('hello world')
print("Set from a string:", char_set)
## Method 3: Creating an empty set
## You must use set() to create an empty set. {} creates an empty dictionary.
empty_set = set()
print("An empty set:", empty_set)
print("Type of empty_set:", type(empty_set))
Save the file. Now, open a terminal in your editor (you can use the menu: Terminal -> New Terminal) and run your script with the following command.
python ~/project/set_basics.py
You will see output similar to the following. Notice that the order of elements in a set is not guaranteed, and duplicates are removed.
Set created with braces: {'cherry', 'apple', 'banana'}
Type of my_set: <class 'set'>
Set with duplicates: {'banana', 'apple'}
Set from a string: {'d', 'l', 'o', 'r', 'w', ' ', 'h', 'e'}
An empty set: set()
Type of empty_set: <class 'set'>
Next, let's add new elements to an existing set. You can add a single element with the add() method or multiple elements with the update() method.
Add the following code to the bottom of your set_basics.py file.
## --- Adding elements ---
fruits = {'apple', 'banana'}
print("\nOriginal fruits set:", fruits)
## Use add() to add a single element
fruits.add('orange')
print("After adding 'orange':", fruits)
## add() has no effect if the element is already present
fruits.add('apple')
print("After adding 'apple' again:", fruits)
## Use update() to add multiple elements from an iterable (like a list)
fruits.update(['mango', 'grape'])
print("After updating with a list:", fruits)
Save the file again and run the updated script from the terminal.
python ~/project/set_basics.py
The output will now include the results of adding elements.
Set created with braces: {'cherry', 'apple', 'banana'}
Type of my_set: <class 'set'>
Set with duplicates: {'banana', 'apple'}
Set from a string: {'d', 'l', 'o', 'r', 'w', ' ', 'h', 'e'}
An empty set: set()
Type of empty_set: <class 'set'>
Original fruits set: {'banana', 'apple'}
After adding 'orange': {'banana', 'orange', 'apple'}
After adding 'apple' again: {'banana', 'orange', 'apple'}
After updating with a list: {'grape', 'mango', 'banana', 'orange', 'apple'}
You have now learned how to create sets and modify them by adding new elements.