{"id":4435,"date":"2023-10-25T18:37:40","date_gmt":"2023-10-25T13:07:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=4435"},"modified":"2023-11-10T11:59:47","modified_gmt":"2023-11-10T06:29:47","slug":"python-set-add-vs-update","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-add-vs-update\/","title":{"rendered":"Python Set: add() vs update()"},"content":{"rendered":"
A set in Python is a data structure that is analogous to a set in mathematics. It can have any number of elements; the order of the elements in a set is undefined. You can add and delete elements from a set, iterate through the set’s elements, and perform standard set operations (union, intersection, difference). Aside from that, you can see if an element is a member of a set.<\/p>\n
In contrast to arrays, where the elements are stored in an ordered list, the order of elements in a set is undefined (moreover, the set elements are usually not stored in order of appearance in the set; this allows checking if an element belongs to a set faster than just going through all the elements of the set).<\/p>\n
In this article, we will look at the main differences between Python’s add() and update() functions for Set.<\/p>\n
If the element is not already present in the set, the set add() method adds it.<\/p>\n
Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n set’s update() function adds elements from another set (passed as an argument) to the set.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n We will discuss this one by one<\/p>\n i)adding single element using add()<\/strong><\/p>\n We can only add one element to the set using the add() function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)adding multiple elements using update()<\/strong><\/p>\n In contrast, the update() function can be used to add multiple elements to the set in a single line.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Add() adds a single element to the set, whereas update() iterates over the given sequences and adds them to the set. As a result, add() outperforms the update() function in terms of performance.<\/p>\n i)add() function<\/strong><\/p>\n The add() function accepts immutable arguments, which means that we can pass int, strings, bytes, frozen sets, tuples, or any other immutable object to it.<\/p>\n So, if we try to pass a mutable object, such as a list, to the add() function, we will get an error.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)update() function<\/strong><\/p>\n The update() function only accepts iterable sequences. If we pass a list to the update() function, it will add all of the elements in the list to the set.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can only pass one argument to the add() function, and it will add that element to the set. In contrast, we can pass multiple arguments, i.e. multiple iterable sequences, when calling the update() function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n A set in Python is a data structure that is analogous to a set in mathematics. It can have any number of elements; the order of the elements in a set is undefined. You can add and delete elements from a set, iterate through the set’s elements, and perform standard set operations (union, intersection, difference). …<\/p>\nset.add(element)<\/code><\/p>\n
add() accepts a single parameter (element) that must be added to the set.<\/pre>\n
The add() method does not return any value.<\/pre>\n
2)update() function<\/h3>\n
set.update(*args)<\/code><\/p>\n
The Update() method only accepts one argument. A set, list, tuples, or dictionary can be used as the single argument. It converts and adds to the set automatically.<\/pre>\n
This method simply adds args to set and returns null.<\/pre>\n
3) add() vs update() difference<\/h3>\n
\n
4)Difference #1:Number of items to be added to set<\/h3>\n
# given set\r\ngivenset = {'hello', 'this', 'is', 'BTeechGeeks'}\r\n# adding element to set using add()\r\ngivenset.add('Python')\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
{'hello', 'BTeechGeeks', 'this', 'is', 'Python'}<\/pre>\n
# given set\r\ngivenset = {'hello', 'this', 'is', 'BTeechGeeks'}\r\n# adding elements to set using update()\r\ngivenset.update(['Python', '2021', 'platform'])\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
{'2021', 'platform', 'is', 'BTeechGeeks', 'this', 'Python', 'hello'}<\/pre>\n
5)Difference #2:Execution time<\/h3>\n
6)Difference #3: Mutable and immutable parameters in add() and update() function<\/h3>\n
# given set\r\ngivenset = {'hello', 'this', 'is', 'BTeechGeeks'}\r\n# adding elements to set using add()\r\ngivenset.add(['Python', '2021', 'platform'])\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/520c50fb7c3daaa8db99bf168af58f53.py\", line 4, in <module>\r\n givenset.add(['Python', '2021', 'platform'])\r\nTypeError: unhashable type: 'list'<\/pre>\n
# given set\r\ngivenset = {'hello', 'this', 'is', 'BTeechGeeks'}\r\n# adding elements to set using update()\r\ngivenset.update(['Python', '2021', 'platform'])\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
{'2021', 'platform', 'is', 'BTeechGeeks', 'this', 'Python', 'hello'}<\/pre>\n
7)Difference #4:Adding multiple elements using update<\/h3>\n
# given set\r\ngivenset = {'hello', 'this', 'is', 'BTeechGeeks'}\r\n# adding elements to set using update()\r\ngivenset.update(['Python', '2021'], ['platform', 'online'], ('India', 122))\r\n# print the set\r\nprint(givenset)\r\n<\/pre>\n
{'hello', 'is', '2021', 'platform', 'online', 122, 'India', 'Python', 'this', 'BTeechGeeks'}<\/pre>\n
\n