banner



How To Add Elements To A Set In Python

Sets in python are used to shop unique elements or objects. Unlike other information structures like tuples or lists, sets practice non allow adding indistinguishable values to them. In this commodity, we volition look at different means to add an element to a gear up in python. We will besides look at some of the use cases where we are not allowed to add together specific types of objects to a set.

Add together an element to a Set using Add together() method

The add() method is used to add new values to a set. When invoked on a set, the add() method takes a unmarried element to be added to the fix equally an input parameter and adds it to the set. When the input element is already present in the set, null happens. After successful execution, The add together() method returns None.

Nosotros tin add an element to a set using the add() method every bit follows.

            mySet = set([ane, 2, 3, iv, v]) print("Original Fix is:", mySet) mySet.add(half-dozen) print("Set subsequently adding 6 to it:", mySet)                      

Output:

            Original Set is: {1, ii, three, iv, 5} Set later on calculation 6 to it: {1, 2, three, 4, five, 6}          

When we add together a duplicate element to the set, the set remains unchanged. You can run across information technology in the post-obit example.

            mySet = set up([1, 2, 3, iv, five]) print("Original Set is:", mySet) mySet.add(5) impress("Set after calculation five to information technology:", mySet)          

Output:

            Original Set is: {1, 2, 3, 4, 5} Set up after calculation five to information technology: {one, two, 3, 4, five}          

How to add multiple elements to a Set up

We volition utilize the update() method to add together multiple elements to a fix. The update() method takes one or more iterable objects such as a python dictionary, tuple, listing, or set and adds the elements of the iterable to the existing set up.

We tin add elements of a listing to a set as follows.

            mySet = set up([one, ii, 3, 4, 5]) impress("Original Fix is:", mySet) myList = [6, 7, 8] print("List of values is:", myList) mySet.update(myList) print("Set after adding elements of myList:", mySet)          

Output:

            Original Set is: {one, 2, 3, four, 5} List of values is: [vi, 7, viii] Prepare later adding elements of myList: {1, 2, 3, 4, 5, half-dozen, 7, eight}                      

To add elements from two or more lists, we simply pass each list as an input argument to the update() method as follows.

            mySet = fix([one, two, iii, 4, five]) print("Original Prepare is:", mySet) myList1 = [6, 7, viii] myList2 = [9, 10] print("First List of values is:", myList1) print("2d Listing of values is:", myList2) mySet.update(myList1, myList2) print("Set up later on adding elements of myList1 and myList2 :", mySet)                      

Output:

            Original Set is: {1, 2, 3, iv, 5} Commencement List of values is: [6, 7, 8] 2nd List of values is: [ix, 10] Set after adding elements of myList1 and myList2 : {1, 2, 3, 4, 5, six, 7, 8, 9, 10}          

Just as lists, we can add elements from one or more tuples or sets using the same syntax which is used for lists.

When we try to add a python dictionary to a fix using the update() method, only the keys of the dictionary are added to the set up. This can exist observed in the post-obit example.

            mySet = set([one, 2, 3, 4, five]) print("Original Set is:", mySet) myDict = {half-dozen: 36, 7: 49, 8: 64} print("Lexicon is:", myDict) mySet.update(myDict) print("Set later on updating :", mySet)          

Output:

            Original Set is: {i, 2, three, 4, 5} Dictionary is: {half dozen: 36, 7: 49, 8: 64} Set later on updating : {1, 2, 3, iv, 5, 6, 7, eight}          

To add the values in a dictionary to the set up, we will take to pass the list of values explicitly by using dict.values() method equally follows.

            mySet = fix([ane, 2, 3, 4, 5]) print("Original Ready is:", mySet) myDict = {6: 36, vii: 49, eight: 64} print("Dictionary is:", myDict) mySet.update(myDict.values()) print("Set later on updating :", mySet)          

Output:

            Original Set is: {1, 2, iii, four, 5} Dictionary is: {6: 36, vii: 49, eight: 64} Set after updating : {64, ane, 2, 3, 4, five, 36, 49}          

Nosotros tin also utilize the unpacking operator * to add multiple elements to a set. For this, we will first unpack the current set and the object containing the elements which are to be added to the set. After unpacking, we tin create a new prepare using all the elements as follows.

            mySet = gear up([1, 2, 3, 4, 5]) print("Original Gear up is:", mySet) myList = [6, 7, 8] print("List of values is:", myList) mySet = {*mySet, *myList} impress("Set later on updating :", mySet)          

Output:

            Original Set is: {ane, ii, 3, 4, 5} List of values is: [6, 7, viii] Prepare after updating : {1, 2, 3, four, 5, 6, 7, 8}          

Adding objects to a set

Merely like individual elements, we can also add objects to a gear up using the add() method. The merely status is that we can add just immutable objects. For example, nosotros tin can add a tuple to a list using the add together() method as follows.

            mySet = set([1, 2, three, 4, five]) impress("Original Set is:", mySet) myTuple = (half dozen, vii, eight) print("List of values is:", myTuple) mySet.add(myTuple) print("Fix after updating :", mySet)                      

Output:

            Original Set is: {1, two, 3, 4, 5} List of values is: (6, 7, 8) Ready afterwards updating : {one, ii, 3, 4, 5, (vi, 7, 8)}          

When nosotros try to add a mutable object such as a list to the set, it raises TypeError. This is due to the reason that mutable objects are non hashable and cannot exist added to the set.

            mySet = set([i, 2, 3, 4, five]) print("Original Fix is:", mySet) myList = [six, 7, eight] print("Listing of values is:", myList) mySet.add together(myList) print("Set up later updating :", mySet)          

Output:

            Original Set is: {i, 2, 3, 4, 5} List of values is: [vi, vii, 8] Least altitude of vertices from vertex 0 is: {0: 0, one: 1, 2: 2, 3: 1, 4: 2, 5: iii} Traceback (most contempo call last):   File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 5, in <module>     mySet.add together(myList) TypeError: unhashable type: 'list'          

 The TypeError tin can be handled past exception handling using python try except blocks.

How to add a cord as element to a prepare in Python

We can add an entire cord to a gear up using the add() method as follows.

            mySet = set([1, 2, 3, 4, v]) print("Original Set is:", mySet) myStr="PythonForBeginners" print("String is:", myStr) mySet.add(myStr) impress("Set subsequently updating :", mySet)                      

Output:

            Original Set is: {1, 2, three, 4, 5} String is: PythonForBeginners Set after updating : {1, two, three, 4, 5, 'PythonForBeginners'}          

To add the characters of the string to the set, we volition utilize the update() method equally follows.

            mySet = set([one, 2, 3, iv, 5]) print("Original Set is:", mySet) myStr="PythonForBeginners" print("String is:", myStr) mySet.update(myStr) print("Set subsequently updating :", mySet)                      

Output:

            Original Set is: {1, 2, 3, iv, 5} String is: PythonForBeginners Prepare subsequently updating : {ane, 2, three, 4, 5, 'P', 'y', 'F', 'r', 'g', 'B', 's', 'i', 'o', 'h', 'n', 't', 'e'}          

Determination

In this article, we have seen various ways to add i or more elements to a set in python. Nosotros have also seen how we can add strings or other immutable objects to the set up. Stay tuned for more than informative articles.

Recommended Python Training

Course: Python 3 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 Elements To A Set In Python,

Source: https://www.pythonforbeginners.com/basics/how-to-add-an-element-to-a-set-in-python

Posted by: plunkettlailme.blogspot.com

0 Response to "How To Add Elements To A Set In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel