In this tutorial, you’ll learn how to append a value to a set in Python using the .add()
and .update()
methods. You’ll learn a brief overview of what Python sets are. Then, you’ll learn how to add a single item to a set using the .add()
method as well as multiple items using the .update()
method. You’ll also learn how to use the concatenation operator to add items to a set.
Quick Answer: Use add
and update
to Append Items to a Set
Table of Contents
What are Python Sets?
Python sets are a data collections that are similar to, say, Python lists and Python dictionaries. They do have some unique attributes: namely, Python sets are
- Unordered, meaning that you can’t index a set
- Contain only unique items, meaning that there cannot be any duplicates in the set.
Sets are created using curly braces ({}
), which may seem similar to Python dictionaries. Because of the similar declaration, you need to use the set()
function to create an empty set.
Let’s look at how we can create a set in Python:
# Creating an Empty Set
a_set = {}
# Creating a Set with Data
another_set = {1, 2, 3, 4}
Because Python sets are unordered, we can’t simply modify an item using an index. We can, however, use set methods to add or remove items from a set. Let’s dive into how to add a single item to a set.
Add an Item to a Python Set using Add
The easiest way to add a single item to a Python set is by using the Python set method, .add()
. The method acts on a set and takes a single parameter, the item to add. The item being added is expected to be an immutable object, such as a string or a number.
Let’s see what this look like:
# Append to a Python set with .add()
items = {1, 2, 3}
items.add(4)
print(items)
# Returns: {1, 2, 3, 4}
We can see that the method works in place, allowing us to add an item without needing to re-assign the set to itself.
Now, what happens when we try to add an item that already exists in the set?
# Appending an existing item to a Python set with .add()
items = {1, 2, 3}
items.add(3)
print(items)
# Returns: {1, 2, 3}
Because Python sets can only contain unique items, when an item that already exists is appended to the set, the set remains unchanged.
Now, let’s see what happens when we try to add an iterable object, such as a list to our set:
# Append to a Python set with .add()
items = {1, 2, 3}
items.add([4, 5, 6])
print(items)
# Returns: TypeError: unhashable type: 'list'
We can see that when we try to append a mutable object, such as a list, that a TypeError
is raised. This is because sets can only contain immutable data types, as they cannot be allowed to be changed.
In many cases, you’ll want to add multiple items to a set. That’s exactly what you’ll learn in the next section.
Add Multiple Items to a Python Set using Update
The Python .add()
method only accepts a single item. What happens when you want to append multiple items to a set? While it’s possible to write a for loop to append multiple items using the .add()
method, Python also provides the .update()
method that accepts an iterable object to append multiple items to a set.
Let’s see what this looks like:
# Appending Multiple Items to a Python Set
items = {1, 2, 3}
new_items = [4, 5, 6]
# You could use a for loop with .add()
for item in new_items:
items.add(item)
print(items)
# Returns: {1, 2, 3, 4, 5, 6}
# Or you could use the .update() method
items = {1, 2, 3}
new_items = [4, 5, 6]
items.update(new_items)
print(items)
# Returns: {1, 2, 3, 4, 5, 6}
We can see that the by using the .update()
adds multiple items easily to a set.
Similar to using the .add()
method, if we try to append items that already exist, they will simply be ignored. We can confirm this by trying it out:
# Appending Multiple Items to a Python Set
items = {1, 2, 3}
new_items = [2, 3, 4, 5, 6]
items.update(new_items)
print(items)
# Returns: {1, 2, 3, 4, 5, 6}
In the next section, you’ll learn how strings can be appended to a Python set.
Adding Strings to Python Sets
An interesting note about appending strings to Python sets is that they can be appended using both the .add()
and .update()
methods. Depending on which one you choose will yield different results. This is because Python strings are technically immutable, but iterable objects.
Because of this, when a string is added using the .add()
method, the string is added as a single item. However, when a string is added using the .update()
method, it’s added as separate items from the string. Let’s see what this looks like:
# Appending a string to a set in Python
items1 = {1, 2, 3}
items2 = {1, 2, 3}
word = 'datagy'
items1.add(word)
items2.update(word)
print('items1 = ', items1)
print('items2 = ', items2)
# Returns:
# items1 = {'datagy', 1, 2, 3}
# items2 = {1, 2, 3, 'a', 'd', 't', 'g', 'y'}
A way that we can modify this behaviour is by passing in the string as a list into the .update()
method. This way, Python will interpret the string as an item in the list, not as the iterable object itself. Let’s confirm this:
# Appending a string to a set in Python
items = {1, 2, 3}
word = 'datagy'
items.update([word])
print(items)
# Returns: {1, 2, 3, 'datagy'}
In the next section, you’ll learn how to use the Python concatenation operator to append to a set in Python.
Use the Concatenation Operator to Append to a Set
Python also comes with a concatenation operator, |=
, which allows us to easily concatenate items. We can combine two sets together using this operator, which results in a set that contains the items of the first and the second set.
Let’s see how this works by appending one set to another:
# Concatenating two sets in Python
items = {1, 2, 3}
more_items = {3, 4, 5}
items |= more_items
print(items)
# Returns: {1, 2, 3, 4, 5}
Here, we instantiated two sets. We then applied the concatenation to one of the sets to append the other, in place.
Conclusion
In this tutorial, you learned how to append to a set in Python. You learned how to use the .add()
and .update()
methods to add an item or multiple items to a set. Then, you learned the quirks of working with strings and how to properly append them to a set. Finally, you learned how the Python concatenation operator |=
works in appending one set to another.
To learn more about the Python .add()
and .update()
methods, check out the official documentation here.
Additional Resources
To learn more about related topics, check out these articles:
After attempting to run:
# Appending Multiple Items to a Python Set
items = {1, 2, 3}
new_items = [4, 5, 6]
# You could use a for loop with .add()
for item in new_items:
items.add(item)
print(items)
# Returns: {1, 2, 3, 4, 5, 6}
I keep getting:
items = {1,2,3,}
new_items = [4,5,6]
for item in new_items:
items.add(item)
print(items)
SyntaxError: invalid syntax
I’m using Python 3.10 IDLE Shell, so it could be that? The second method works. What could be the issue. Guarantee it’s not PICNIC .
Okay, I did the same thing in the IDLE, and it worked.
Must’ve been the shell…
Seen that before, so yeah.
Anyway. Cheers
I’m glad it worked, Thomas! 🙂
Thanks for the guide, it was really helpful.
I’ve got one minor improvement idea, because it caught me off guard:
Remove the code that says:
# Creating an Empty Set
a_set = {}
This code does not actually create a set and you mention it in the text above the code, but a fast reader might miss that part and run into an error 😉
That’s a great idea, Daniel! Thank you! I’ll update that when I go through the post next.