How To Add Element To Set Python
Sets in python are used to store unique elements or objects. Unlike other data structures like tuples or lists, sets do not allow adding duplicate values to them. In this article, nosotros will expect at dissimilar ways to add an element to a set up in python. We will as well look at some of the employ cases where we are not allowed to add specific types of objects to a prepare.
Add an element to a Prepare using Add() method
The add() method is used to add together new values to a set. When invoked on a set, the add() method takes a single element to be added to the set up as an input parameter and adds information technology to the set. When the input element is already present in the gear up, zippo happens. After successful execution, The add() method returns None.
We can add together an element to a ready using the add together() method every bit follows.
mySet = prepare([one, 2, 3, 4, five]) print("Original Set is:", mySet) mySet.add(6) print("Set up after calculation 6 to it:", mySet)
Output:
Original Set is: {1, 2, 3, 4, 5} Set later on calculation six to it: {1, 2, 3, 4, 5, 6}
When nosotros add a duplicate element to the prepare, the gear up remains unchanged. You can see it in the post-obit example.
mySet = set([i, 2, 3, 4, 5]) impress("Original Gear up is:", mySet) mySet.add together(5) print("Prepare after calculation 5 to information technology:", mySet)
Output:
Original Set is: {1, ii, three, 4, 5} Set afterward calculation 5 to it: {1, 2, iii, 4, v}
How to add multiple elements to a Set
We volition use the update() method to add multiple elements to a set. The update() method takes ane or more iterable objects such equally a python dictionary, tuple, list, or set and adds the elements of the iterable to the existing gear up.
We can add together elements of a listing to a set as follows.
mySet = ready([1, two, 3, 4, 5]) print("Original Fix is:", mySet) myList = [6, 7, viii] print("Listing of values is:", myList) mySet.update(myList) impress("Set subsequently adding elements of myList:", mySet)
Output:
Original Set is: {1, 2, iii, 4, 5} List of values is: [six, 7, eight] Ready after adding elements of myList: {ane, two, 3, 4, 5, 6, 7, 8}
To add elements from 2 or more than lists, we simply laissez passer each list every bit an input argument to the update() method equally follows.
mySet = set([1, 2, three, 4, 5]) print("Original Gear up is:", mySet) myList1 = [6, 7, eight] myList2 = [nine, 10] print("Beginning Listing of values is:", myList1) print("Second List of values is:", myList2) mySet.update(myList1, myList2) impress("Prepare later on calculation elements of myList1 and myList2 :", mySet)
Output:
Original Set up is: {1, 2, 3, 4, v} First Listing of values is: [6, 7, eight] 2d List of values is: [9, 10] Set afterwards adding elements of myList1 and myList2 : {1, 2, 3, 4, 5, 6, 7, viii, 9, 10}
But as lists, we can add elements from i or more tuples or sets using the same syntax which is used for lists.
When we endeavor to add a python lexicon to a gear up using the update() method, only the keys of the lexicon are added to the fix. This tin can exist observed in the post-obit example.
mySet = gear up([1, 2, iii, 4, 5]) print("Original Set is:", mySet) myDict = {six: 36, 7: 49, viii: 64} print("Dictionary is:", myDict) mySet.update(myDict) print("Set after updating :", mySet)
Output:
Original Set is: {1, 2, three, 4, 5} Dictionary is: {6: 36, 7: 49, eight: 64} Fix after updating : {one, 2, 3, 4, 5, half dozen, vii, 8}
To add together the values in a dictionary to the set, we volition have to pass the list of values explicitly past using dict.values() method as follows.
mySet = set([i, ii, 3, iv, five]) print("Original Set is:", mySet) myDict = {6: 36, 7: 49, 8: 64} print("Lexicon is:", myDict) mySet.update(myDict.values()) impress("Set after updating :", mySet)
Output:
Original Set is: {1, 2, 3, iv, 5} Dictionary is: {6: 36, vii: 49, viii: 64} Gear up later updating : {64, i, 2, 3, four, 5, 36, 49}
Nosotros can also use the unpacking operator * to add multiple elements to a fix. For this, we will first unpack the electric current fix and the object containing the elements which are to be added to the set. After unpacking, we can create a new ready using all the elements every bit follows.
mySet = prepare([1, 2, iii, iv, 5]) print("Original Set is:", mySet) myList = [6, 7, 8] impress("List of values is:", myList) mySet = {*mySet, *myList} print("Fix after updating :", mySet)
Output:
Original Set is: {1, 2, 3, 4, five} List of values is: [6, vii, 8] Ready after updating : {1, 2, 3, 4, 5, 6, 7, 8}
Adding objects to a set
Just similar individual elements, nosotros tin likewise add objects to a set using the add() method. The but status is that nosotros can add simply immutable objects. For example, nosotros can add a tuple to a list using the add together() method as follows.
mySet = set([1, 2, 3, 4, 5]) print("Original Set is:", mySet) myTuple = (6, seven, 8) print("Listing of values is:", myTuple) mySet.add together(myTuple) print("Set afterward updating :", mySet)
Output:
Original Set is: {1, ii, iii, four, 5} List of values is: (6, 7, 8) Set later updating : {1, two, 3, 4, 5, (6, vii, 8)}
When we endeavor to add a mutable object such every bit a list to the ready, it raises TypeError. This is due to the reason that mutable objects are not hashable and cannot be added to the set up.
mySet = set([1, 2, 3, 4, v]) impress("Original Set is:", mySet) myList = [vi, 7, 8] print("List of values is:", myList) mySet.add(myList) print("Prepare after updating :", mySet)
Output:
Original Prepare is: {one, 2, three, 4, 5} List of values is: [vi, seven, viii] Least altitude of vertices from vertex 0 is: {0: 0, 1: ane, 2: two, 3: 1, 4: 2, 5: 3} Traceback (most contempo telephone call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line v, in <module> mySet.add(myList) TypeError: unhashable type: 'listing'
The TypeError can be handled by exception handling using python try except blocks.
How to add a cord as chemical element to a gear up in Python
We can add an unabridged string to a fix using the add together() method as follows.
mySet = gear up([one, two, iii, iv, 5]) print("Original Gear up is:", mySet) myStr="PythonForBeginners" impress("String is:", myStr) mySet.add(myStr) print("Fix subsequently updating :", mySet)
Output:
Original Set is: {1, ii, 3, four, 5} String is: PythonForBeginners Prepare afterwards updating : {ane, 2, 3, four, v, 'PythonForBeginners'}
To add the characters of the string to the gear up, nosotros will use the update() method as follows.
mySet = prepare([one, 2, 3, 4, five]) print("Original Gear up is:", mySet) myStr="PythonForBeginners" print("String is:", myStr) mySet.update(myStr) print("Gear up later on updating :", mySet)
Output:
Original Set up is: {1, ii, 3, 4, five} String is: PythonForBeginners Set later updating : {1, 2, 3, four, 5, 'P', 'y', 'F', 'r', 'g', 'B', 's', 'i', 'o', 'h', 'n', 't', 'eastward'}
Conclusion
In this article, we have seen various means to add one or more elements to a set in python. We have as well seen how we can add together strings or other immutable objects to the set. Stay tuned for more informative manufactures.
Recommended Python Grooming
Course: Python iii For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.
How To Add Element To Set Python,
Source: https://www.pythonforbeginners.com/basics/how-to-add-an-element-to-a-set-in-python
Posted by: hickscolithat.blogspot.com
0 Response to "How To Add Element To Set Python"
Post a Comment