banner



How To Use Set In Python 3

Sets in python are used to store unique elements or objects. Different other data structures similar tuples or lists, sets practice not allow calculation duplicate values to them. In this article, we volition wait at different ways to add an element to a set in python. Nosotros will also expect at some of the apply cases where nosotros are not allowed to add specific types of objects to a fix.

Add 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 up as an input parameter and adds it to the fix. When the input element is already nowadays in the fix, nothing happens. After successful execution, The add() method returns None.

Nosotros can add together an element to a set using the add() method as follows.

            mySet = set([1, 2, 3, 4, 5]) print("Original Set is:", mySet) mySet.add(vi) print("Set after calculation half-dozen to it:", mySet)                      

Output:

            Original Set is: {one, ii, iii, 4, five} Gear up after calculation half-dozen to it: {1, 2, 3, four, 5, half dozen}          

When we add a duplicate chemical element to the set, the set up remains unchanged. You can see information technology in the following instance.

            mySet = set([1, 2, 3, iv, 5]) print("Original Set is:", mySet) mySet.add together(v) print("Set after adding five to it:", mySet)          

Output:

            Original Set is: {one, 2, 3, four, 5} Set after adding 5 to it: {1, 2, three, 4, 5}          

How to add multiple elements to a Set up

We volition use the update() method to add multiple elements to a prepare. The update() method takes one or more iterable objects such every bit a python dictionary, tuple, list, or set and adds the elements of the iterable to the existing set.

Nosotros tin can add elements of a list to a set as follows.

            mySet = set([1, two, 3, four, 5]) print("Original Set is:", mySet) myList = [half dozen, vii, eight] print("Listing of values is:", myList) mySet.update(myList) print("Prepare after adding elements of myList:", mySet)          

Output:

            Original Set is: {ane, two, three, iv, 5} Listing of values is: [vi, 7, viii] Set later on adding elements of myList: {1, 2, 3, 4, 5, 6, seven, 8}                      

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

            mySet = set([1, 2, 3, 4, 5]) print("Original Set is:", mySet) myList1 = [6, 7, eight] myList2 = [ix, ten] print("First Listing of values is:", myList1) print("Second List of values is:", myList2) mySet.update(myList1, myList2) print("Set afterwards calculation elements of myList1 and myList2 :", mySet)                      

Output:

            Original Set up is: {1, ii, 3, 4, 5} First Listing of values is: [six, 7, viii] 2nd List of values is: [9, 10] Set after calculation elements of myList1 and myList2 : {one, 2, 3, 4, five, 6, vii, 8, 9, 10}          

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

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

            mySet = set([1, two, 3, 4, 5]) print("Original Set is:", mySet) myDict = {6: 36, 7: 49, 8: 64} print("Dictionary is:", myDict) mySet.update(myDict) impress("Set after updating :", mySet)          

Output:

            Original Set is: {one, 2, three, four, 5} Dictionary is: {vi: 36, 7: 49, 8: 64} Set after updating : {1, 2, 3, 4, 5, six, 7, viii}          

To add the values in a lexicon to the set, nosotros will take to pass the list of values explicitly by using dict.values() method as follows.

            mySet = set([1, 2, three, 4, 5]) impress("Original Set is:", mySet) myDict = {6: 36, 7: 49, eight: 64} print("Dictionary is:", myDict) mySet.update(myDict.values()) impress("Set later updating :", mySet)          

Output:

            Original Fix is: {1, 2, iii, four, 5} Lexicon is: {6: 36, 7: 49, eight: 64} Set later updating : {64, ane, two, three, 4, 5, 36, 49}          

We tin can likewise use the unpacking operator * to add together multiple elements to a set. For this, we volition get-go unpack the electric current prepare and the object containing the elements which are to be added to the set. After unpacking, we can create a new fix using all the elements as follows.

            mySet = set([1, 2, iii, 4, 5]) print("Original Set is:", mySet) myList = [6, seven, 8] print("List of values is:", myList) mySet = {*mySet, *myList} print("Ready after updating :", mySet)          

Output:

            Original Set is: {one, 2, iii, 4, 5} Listing of values is: [6, seven, viii] Ready subsequently updating : {1, 2, 3, 4, 5, half dozen, vii, eight}          

Calculation objects to a prepare

Just like individual elements, we tin can likewise add together objects to a ready using the add() method. The simply status is that we tin can add only immutable objects. For case, we can add a tuple to a list using the add() method as follows.

            mySet = set([1, two, 3, 4, 5]) print("Original Set is:", mySet) myTuple = (6, vii, eight) print("Listing of values is:", myTuple) mySet.add(myTuple) print("Set after updating :", mySet)                      

Output:

            Original Set is: {1, 2, three, 4, 5} List of values is: (half-dozen, seven, viii) Set after updating : {1, ii, 3, four, five, (6, 7, 8)}          

When nosotros try to add together a mutable object such equally a listing to the fix, information technology raises TypeError. This is due to the reason that mutable objects are non hashable and cannot be added to the set.

            mySet = prepare([i, 2, three, 4, 5]) print("Original Ready is:", mySet) myList = [6, 7, 8] print("Listing of values is:", myList) mySet.add(myList) print("Set afterward updating :", mySet)          

Output:

            Original Set is: {1, 2, 3, 4, 5} List of values is: [half-dozen, 7, 8] Least distance of vertices from vertex 0 is: {0: 0, 1: i, two: 2, 3: 1, 4: 2, v: 3} Traceback (most recent phone call final):   File "/habitation/aditya1117/PycharmProjects/pythonProject/string1.py", line v, in <module>     mySet.add(myList) TypeError: unhashable blazon: 'list'          

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

How to add together a string equally chemical element to a set in Python

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

            mySet = set([ane, 2, 3, 4, 5]) impress("Original Ready is:", mySet) myStr="PythonForBeginners" impress("Cord is:", myStr) mySet.add(myStr) impress("Gear up later on updating :", mySet)                      

Output:

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

To add the characters of the cord to the prepare, we will use the update() method as follows.

            mySet = set up([one, two, iii, iv, 5]) print("Original Gear up is:", mySet) myStr="PythonForBeginners" print("Cord is:", myStr) mySet.update(myStr) print("Set afterward updating :", mySet)                      

Output:

            Original Set is: {ane, 2, 3, four, 5} Cord is: PythonForBeginners Prepare after updating : {1, 2, three, four, v, 'P', 'y', 'F', 'r', 'yard', 'B', 'southward', 'i', 'o', 'h', 'due north', 't', 'east'}          

Conclusion

In this article, nosotros have seen various ways to add 1 or more than elements to a set in python. We have likewise seen how we tin add strings or other immutable objects to the set. Stay tuned for more than informative articles.

Recommended Python Preparation

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 Use Set In Python 3,

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

Posted by: cuevasfroper.blogspot.com

0 Response to "How To Use Set In Python 3"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel