How To Append Set In Python
In this tutorial, you'll learn how to suspend a value to a fix in Python using the .add()
and .update()
methods. Yous'll acquire a brief overview of what Python sets are. Then, you'll learn how to add a unmarried detail to a gear up using the .add together()
method as well as multiple items using the .update()
method. Y'all'll also acquire how to apply the concatenation operator to add together items to a gear up.
Quick Answer: Utilize add
and update
to Append Items to a Gear up

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, significant that you tin't index a set
- Contain simply unique items, meaning that there cannot be whatsoever duplicates in the gear up.
Sets are created using curly braces ({}
), which may seem similar to Python dictionaries. Because of the similar declaration, yous need to use the set()
office to create an empty ready.
Let's wait at how we can create a set up in Python:
# Creating an Empty Set a_set = {} # Creating a Ready with Data another_set = {1, two, iii, 4}
Considering Python sets are unordered, we can't but change an item using an index. Nosotros can, notwithstanding, use set methods to add or remove items from a set up. Allow'south swoop into how to add a single item to a ready.
Add together an Item to a Python Set using Add together
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 beingness added is expected to be an immutable object, such as a string or a number.
Let's see what this expect like:
# Append to a Python gear up with .add() items = {1, 2, 3} items.add(4) impress(items) # Returns: {i, 2, 3, 4}
Nosotros can see that the method works in identify, allowing us to add together an item without needing to re-assign the set to itself.
At present, what happens when we attempt to add together an item that already exists in the ready?
# Appending an existing item to a Python ready with .add() items = {one, two, iii} items.add together(3) print(items) # Returns: {ane, 2, 3}
Because Python sets tin can only contain unique items, when an item that already exists is appended to the set up, the fix remains unchanged.
Now, allow'south see what happens when we try to add an iterable object, such as a list to our ready:
# Suspend to a Python set with .add together() items = {one, 2, iii} items.add([4, five, vi]) print(items) # Returns: TypeError: unhashable type: 'listing'
Nosotros can see that when we try to suspend a mutable object, such as a list, that a TypeError
is raised. This is because sets can merely contain immutable data types, as they cannot be allowed to be changed.
In many cases, you'll want to add together multiple items to a ready. That's exactly what you'll learn in the next section.
Add Multiple Items to a Python Set up using Update
The Python .add()
method only accepts a single detail. What happens when y'all desire to append multiple items to a fix? While it's possible to write a for loop to append multiple items using the .add()
method, Python likewise provides the .update()
method that accepts an iterable object to suspend multiple items to a set.
Let's see what this looks similar:
# Appending Multiple Items to a Python Set up items = {one, ii, 3} new_items = [4, v, six] # You could use a for loop with .add together() for item in new_items: items.add(detail) print(items) # Returns: {1, two, 3, 4, v, 6} # Or yous could use the .update() method items = {ane, 2, 3} new_items = [4, five, 6] items.update(new_items) print(items) # Returns: {1, 2, iii, 4, 5, half dozen}
Nosotros can see that the by using the .update()
adds multiple items hands to a set.
Similar to using the .add()
method, if nosotros try to suspend items that already be, they will only be ignored. We can ostend this past trying it out:
# Appending Multiple Items to a Python Prepare items = {i, 2, three} new_items = [2, 3, 4, five, vi] items.update(new_items) print(items) # Returns: {i, two, three, iv, 5, 6}
In the next section, you'll larn 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 considering Python strings are technically immutable, but iterable objects.
Considering of this, when a string is added using the .add together()
method, the string is added as a unmarried detail. Notwithstanding, when a string is added using the .update()
method, it's added as carve up items from the string. Let'due south run into what this looks like:
# Appending a string to a set up in Python items1 = {ane, ii, 3} items2 = {ane, 2, 3} word = 'datagy' items1.add(discussion) items2.update(discussion) impress('items1 = ', items1) impress('items2 = ', items2) # Returns: # items1 = {'datagy', ane, ii, 3} # items2 = {i, 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 cord as an item in the listing, non as the iterable object itself. Let's confirm this:
# Appending a cord to a set in Python items = {1, 2, iii} discussion = 'datagy' items.update([word]) print(items) # Returns: {1, 2, three, 'datagy'}
In the next section, yous'll learn how to use the Python chain operator to append to a set in Python.
Apply the Concatenation Operator to Append to a Set
Python likewise 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 commencement and the second gear up.
Allow'due south see how this works by appending one set up to another:
# Concatenating two sets in Python items = {1, 2, iii} more_items = {3, four, v} items |= more_items print(items) # Returns: {i, ii, 3, iv, 5}
Here, we instantiated two sets. We so applied the concatenation to 1 of the sets to append the other, in place.
Decision
In this tutorial, you lot learned how to append to a set in Python. You learned how to apply the .add together()
and .update()
methods to add an detail 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 chain operator |=
works in appending one prepare to another.
To acquire more than well-nigh the Python .add()
and .update()
methods, check out the official documentation here.
Additional Resource
To learn more than about related topics, check out these manufactures:
- Pandas: Count Unique Values in a GroupBy Object
- Python: Count Unique Values in a List (four Ways)
- Python: Count Number of Occurrences in List (6 Ways)
- Python: Remove Duplicates From a List (7 Ways)
How To Append Set In Python,
Source: https://datagy.io/python-append-set/
Posted by: dayjast1956.blogspot.com
0 Response to "How To Append Set In Python"
Post a Comment