{"id":3199,"date":"2021-04-21T12:45:09","date_gmt":"2021-04-21T07:15:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=3199"},"modified":"2021-11-22T18:44:58","modified_gmt":"2021-11-22T13:14:58","slug":"python-how-to-create-an-empty-set-and-append-items-to-it","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-create-an-empty-set-and-append-items-to-it\/","title":{"rendered":"Python: How to Create an Empty Set and Append Items to It?"},"content":{"rendered":"
A Set is an unordered collection data type that can be iterated, mutated, and does not contain duplicate elements. Python’s set class represents the mathematical concept of a set. The main advantage of using a set over a list is that it has a highly optimised method for determining whether a specific element is contained in the set. This is based on the hash table data structure. Because sets are unordered, we cannot access items using indexes as we do in lists.<\/p>\n
Characteristics of set:<\/strong><\/p>\n In this article, we will first look at various ways to create an empty set, and then we will look at how to add or append items to the empty set.<\/p>\n A set is formed by enclosing all of the items (elements) within curly braces, separated by commas, or by using the built-in set() function.<\/p>\n It can contain an unlimited number of items of various types (integer, float, tuple, string etc.). A set, on the other hand, cannot have mutable elements such as lists, sets, or dictionaries as its elements.<\/p>\n It takes an optional iterable sequence and returns a set that has been initialised with the elements in the sequence. However, if no iterable sequence is passed to the function it returns an empty set.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Empty curly brackets, i.e., are commonly used in Python to create a dictionary. However, starting with Python 3.5, if we pass some comma separated arguments in curly square brackets, it will create a set with them. For example, 1, 2, 3 can be used to make a set with three values.<\/p>\n So, to make an empty set, we can use the same method.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In set, insertion is accomplished via the set.add() function, which generates an appropriate record value for storage in the hash table. The same as searching for a specific item, i.e., O(1) on average. However, in the worst-case scenario, it can become O (n).<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Using the set’s update() function, we can add multiple items to an empty set in a single line.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n The update() function takes a single or several iterable sequences as arguments and updates the set with all of the things in those sequences. In the preceding example, we created an empty set and then appended all of the items in a tuple to it in a single line.<\/p>\n A Set is an unordered collection data type that can be iterated, mutated, and does not contain duplicate elements. Python’s set class represents the mathematical concept of a set. The main advantage of using a set over a list is that it has a highly optimised method for determining whether a specific element is contained …<\/p>\n\n
\n
Methods to Create an Empty Set<\/h2>\n
Method #1: Using set() to create an empty set in Python<\/h3>\n
# creating new set\r\nemptyset = set()\r\n# printing the set\r\nprint(emptyset)\r\n# printing the size of empty set\r\nprint(\"length of set = \", len(emptyset))\r\n<\/pre>\n
set()\r\nlength of set = 0<\/pre>\n
Method #2:Using empty set literal<\/h3>\n
# creating new set\r\nemptyset = {*()}\r\n# printing the set\r\nprint(emptyset)\r\n# printing the size of empty set\r\nprint(\"length of set = \", len(emptyset))\r\n<\/pre>\n
set()\r\nlength of set = 0<\/pre>\n
Append an item to an empty set<\/h3>\n
# creating new set\r\nemptyset = {*()}\r\n# adding elements to set\r\nemptyset.add('BTechGeeks')\r\nemptyset.add('Hello')\r\nemptyset.add('BTechGeeks')\r\n# printing the set\r\nprint(emptyset)\r\n<\/pre>\n
{'BTechGeeks', 'Hello'}<\/pre>\n
Appending multiple items to the empty set<\/h3>\n
# creating new set\r\nemptyset = set()\r\n# adding elements to set\r\nemptyset.update(('Hello', 'BTechGeeks', 'Hello'))\r\n# printing the set\r\nprint(emptyset)\r\n<\/pre>\n
{'Hello', 'BTechGeeks'}<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n