banner



How To Add Elements To Set In Python

Sets in python are used to store unique elements or objects. Unlike other information structures similar tuples or lists, sets do non let calculation duplicate values to them. In this commodity, we will look at unlike ways to add together an element to a set in python. We will also await at some of the use cases where we are not allowed to add specific types of objects to a fix.

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

The add() method is used to add new values to a set. When invoked on a set, the add together() method takes a single element to be added to the set as an input parameter and adds it to the ready. When the input element is already nowadays in the set, nothing happens. Afterward successful execution, The add() method returns None.

We can add together an element to a fix using the add() method as follows.

            mySet = set([1, 2, 3, four, 5]) print("Original Prepare is:", mySet) mySet.add(6) impress("Set subsequently adding half dozen to information technology:", mySet)                      

Output:

            Original Set is: {1, 2, 3, iv, 5} Ready afterward adding six to it: {1, 2, 3, 4, v, half-dozen}          

When we add a indistinguishable chemical element to the set, the set remains unchanged. You can meet it in the post-obit case.

            mySet = ready([1, 2, 3, 4, 5]) print("Original Ready is:", mySet) mySet.add(5) print("Set subsequently calculation 5 to information technology:", mySet)          

Output:

            Original Gear up is: {1, ii, three, iv, 5} Set after adding 5 to information technology: {one, ii, three, 4, 5}          

How to add together multiple elements to a Set

We will employ the update() method to add multiple elements to a set. The update() method takes i or more than iterable objects such equally a python dictionary, tuple, list, or set and adds the elements of the iterable to the existing set.

We tin can add elements of a list to a fix as follows.

            mySet = set([one, two, 3, 4, five]) impress("Original Ready is:", mySet) myList = [six, 7, 8] print("List of values is:", myList) mySet.update(myList) impress("Set after adding elements of myList:", mySet)          

Output:

            Original Set is: {i, 2, iii, 4, 5} List of values is: [6, seven, 8] Set afterwards adding elements of myList: {1, two, 3, iv, 5, 6, 7, eight}                      

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

            mySet = set([1, 2, three, 4, 5]) print("Original Set is:", mySet) myList1 = [half-dozen, 7, 8] myList2 = [9, 10] print("Kickoff List of values is:", myList1) print("Second Listing of values is:", myList2) mySet.update(myList1, myList2) print("Set after adding elements of myList1 and myList2 :", mySet)                      

Output:

            Original Prepare is: {1, two, 3, four, five} First List of values is: [six, 7, 8] Second List of values is: [ix, 10] Set after adding elements of myList1 and myList2 : {1, two, iii, iv, 5, half-dozen, 7, 8, nine, ten}          

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

When we try to add together a python dictionary to a set using the update() method, only the keys of the lexicon are added to the set. This can exist observed in the following instance.

            mySet = set([1, 2, iii, 4, 5]) print("Original Ready is:", mySet) myDict = {6: 36, 7: 49, 8: 64} impress("Lexicon is:", myDict) mySet.update(myDict) print("Set afterward updating :", mySet)          

Output:

            Original Set is: {i, 2, 3, 4, 5} Lexicon is: {six: 36, 7: 49, 8: 64} Ready later updating : {ane, two, 3, 4, 5, vi, 7, 8}          

To add together the values in a lexicon to the set, we will have to laissez passer the list of values explicitly by using dict.values() method as follows.

            mySet = set([one, ii, 3, 4, 5]) print("Original Set is:", mySet) myDict = {6: 36, seven: 49, 8: 64} print("Dictionary is:", myDict) mySet.update(myDict.values()) print("Fix after updating :", mySet)          

Output:

            Original Gear up is: {i, 2, 3, four, 5} Dictionary is: {half dozen: 36, 7: 49, 8: 64} Ready afterward updating : {64, one, 2, three, 4, 5, 36, 49}          

Nosotros can likewise use the unpacking operator * to add multiple elements to a ready. For this, we will first unpack the current gear up and the object containing the elements which are to be added to the set. After unpacking, we tin can create a new set using all the elements equally follows.

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

Output:

            Original Set is: {1, two, 3, four, 5} List of values is: [half dozen, 7, 8] Gear up after updating : {ane, 2, 3, 4, v, 6, 7, viii}          

Adding objects to a set

Just like individual elements, nosotros can besides add together objects to a prepare using the add() method. The only condition is that we tin add together only immutable objects. For example, we can add together a tuple to a list using the add() method as follows.

            mySet = set([1, two, three, 4, five]) print("Original Prepare is:", mySet) myTuple = (half dozen, 7, 8) print("Listing of values is:", myTuple) mySet.add(myTuple) impress("Set after updating :", mySet)                      

Output:

            Original Set is: {1, two, 3, iv, five} List of values is: (vi, 7, 8) Set afterward updating : {1, 2, 3, four, 5, (half-dozen, 7, 8)}          

When we attempt 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 be added to the gear up.

            mySet = fix([one, ii, iii, 4, v]) print("Original Set is:", mySet) myList = [6, 7, eight] print("List of values is:", myList) mySet.add(myList) print("Gear up after updating :", mySet)          

Output:

            Original Fix is: {1, 2, iii, four, 5} List of values is: [six, 7, 8] Least distance of vertices from vertex 0 is: {0: 0, 1: one, 2: 2, iii: 1, 4: 2, v: three} Traceback (virtually recent call final):   File "/abode/aditya1117/PycharmProjects/pythonProject/string1.py", line five, in <module>     mySet.add(myList) TypeError: unhashable type: 'list'          

 The TypeError can exist handled by exception treatment using python endeavor except blocks.

How to add a string as element to a set in Python

Nosotros can add together an entire string to a ready using the add together() method equally follows.

            mySet = prepare([ane, 2, 3, iv, 5]) print("Original Set is:", mySet) myStr="PythonForBeginners" print("String is:", myStr) mySet.add together(myStr) print("Gear up after updating :", mySet)                      

Output:

            Original Set is: {1, two, 3, 4, 5} String is: PythonForBeginners Gear up afterward updating : {1, ii, 3, four, 5, 'PythonForBeginners'}          

To add the characters of the string to the gear up, we will use the update() method equally follows.

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

Output:

            Original Set is: {1, 2, 3, 4, five} Cord is: PythonForBeginners Set after updating : {1, two, 3, 4, 5, 'P', 'y', 'F', 'r', 'grand', 'B', 's', 'i', 'o', 'h', 'n', 't', 'eastward'}          

Conclusion

In this commodity, nosotros have seen various ways to add together one or more elements to a set in python. We accept also seen how we can add strings or other immutable objects to the gear up. Stay tuned for more informative articles.

Recommended Python Preparation

Form: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real globe applications and master the basics.

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

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel