Python

Python Set Add

Unordered objects are grouped as a set. Every component of a set would have to be distinct and unchanging. A set itself, therefore, is changeable. To it, we can insert or eliminate the elements. The elements can be combined into a set by using curly brackets, commas to split them, or the inbuilt set() method. It may contain a variety of elements, each of which could be of a specific type. However, mutable items will not be included in a set of items.

As the sets are not sorted, indexing is useless. Indexing will not be used to retrieve or modify a set item. It is not handled by the set data structure. The add() function is being utilized to insert one item and the update() function will be applied to add many items. Let’s discuss how to insert an item into a set in this article.

Example no 1:

Sets are unsorted. Thus, we could not even change an element by its index. But we may insert or delete elements from a set using set operations. The set function add() is the simplest technique to insert a single element into any set. The element to insert is the only argument required by the function, which operates on a set. It is intended that the element being inserted will be an immutable item, like a character or an integer.

a = {33, 44, 55, 66}

a.add(88)

print(a)

In this example, we declare a variable and define some random numbers. Then, we call add() function. This function is being used to insert a new number in the data of variable ‘a’. In the next step, we print the numbers by adding 88 with the help of the print() function.

As we’ve seen, this approach enables us to insert an element without having to change the set’s allocation.

Example no 2:

In this instance, we’ll examine what occurs when we want to insert an already-present element in the set.

x = {100, 200, 300, 400, 500, 600}

x.add(600)

print('After adding the element:', x)

The variable ‘x’ is being initialized. It stores 6 different values. We want to insert that element that is already present in the defined list. So, we will employ the add() method. We want to add 600 again to the list. Next, we call the print() function to display the list after adding the element ‘600’.

Python sets can only include distinct things. The set does not change if an existing element is inserted into the set and it becomes unique later.

Example no 3:

In this case, we are going to observe what will happen when we want to insert a list in any defined set.

m = {2093, 3928, 9279, 6238, 6456}

m.add([45987, 93471, 12937])

print(m)

In this scenario, a variable named “m” is specified. Five random numbers are contained in this variable. The add() method is then called. In the data of variable “a,” a new list is being added using this method. Three new values are present in a new list and we want to insert them. The print() method is used in the following step to add a new list in the set and print the defined set.

We could observe that a TypeError is produced whenever we want to add a mutable item, like a list. This is because the sets will only include unchangeable data structures. After all, they’re not enabled to acquire modifications.

Example no 4:

The add() function only takes one argument. We notice what occurs when we try to add more than one element to a set.  It is feasible to create a for loop that utilizes the add() function to add several elements.

elements = {12321, 65742, 89603, 75997}

new_elements = [45486, 58754]

for i in new_elements:

elements.add(i)

 

print(elements)

After defining the elements, a list named ‘new_elements’ is being initialized. We are going to use for loop. Here, we initialize a loop variable. Within for loop, we apply to the add() function. We pass the value of the loop variable as the argument of the function. To terminate the program, the print() function is utilized to show all the elements.

Example no 5:

We’ll utilize the.update() function to insert many entries quickly and efficiently to a set.

j = {1712, 2238, 31873}

new_num = [4198, 3985, 9056, 2362]

j.update(new_num)

print(j)

At the start of the program, we define some values and these values are stored in the variable ‘j’. Then, we specify some elements which we want to add to the required set. We define these values in the form of a list. Thus, we call the update() function to add the list to the required set. This function contains that list as its parameter. To show the complete set after inserting the list, we will use the print() method.

Example no 6:

The add() and update() functions would both be used to insert the strings. The strings seem to be iterable but formally immutable items. Hence, a string is inserted as a particular element when it is inserted via the add() function. The update() function, on the other hand, inserts strings as distinct elements to the string.

num1 = {48999443, 28764082, 27369723, 862935429}

num2 = {10938044, 17263712, 198034483}

w = 'information'

num1.add(w)

num2.update(w)

print('After applying add function = ', num1)

print('After applying update function = ', num2)

We indicate two variables named ‘num1’ and ‘num2’. These variables store randomized numbers. The variable ‘num1’ contains four numbers and the ‘num2’ has three numbers. In the next step, we specify the word ‘information’ which we want to be added to both lists ‘num1’ and ‘num2’. To add the string stored in the ‘w’ variable, we apply first the add() function on the list ‘num1’. Then, we call the update() function for ‘num2’. Now, we use the print() function to depict both lists after adding the string.

Conclusion

We have acquired a basic introduction to Python sets in this guide. Then, we discussed how to utilize the add() function to insert only one element to a set as well as the update() function to insert several elements. We additionally noticed the use of a “for” loop to add a list to a set of values. Furthermore, we learned the peculiarities of interacting with strings and how to insert the strings into a set in an effective manner.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content