Introduction
This is another article which is focusing on how to use a certain type of collection data type. In this article, the focus is to show how to use set in Python programming language. Beside list, tuple and dictionary, there is also another collection data type which is called ‘Set’. So, this article will show how to be able to utilize or to use set in Python programming language.
How to Use Set in Python
So, in this part, it will sequentially to to list and describe several aspects which is going to be available in set for further usage in Python programming language. Those aspects starts from declaring and defining set, adding data into set, removing data in set and also replacing any existing data in set.
Declaring and Defining Set
First of all, the first important aspect is declaring and defining set. In order to do that, there are two ways for defining set. The first one is a way for declaring or defining set without any data at all. In order to do that, the following is an example in order to be able to do that :
set_example = {}
Actually, it seems there is no different between declaring or defining dictionary. Set is also using a curly bracket as dictionary. But the main difference is that set does not use any index or identifier such as key in the form of string. But if the declaration or definition of set includes data in it, it will have a very different presentation. The following is the second declaration or definition of set which include additional data in it :
set_example = {} (env) C:\python>python Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> set_example = {1, 2, 3, 4} >>> print(set_example); {1, 2, 3, 4} >>>
Adding Data in Set
After declaring or defining set, the possible aspect after which in this context the usage of set is just by adding a new data. The following is the actual process for adding data to a set. Adding data to a set exist in the following sequence continuing from the previous aspect which is declaring or defining set using several data :
>>> set_example.add(5); >>> set_example.add(6); >>> set_example.add(7); >>> print(set_example); {1, 2, 3, 4, 5, 6, 7}
Removing Data in Set
Another part as an aspect for collection data type which in this context is a set, it is the utility or the usage to remove the data in it. Actually, the following is a method available in set which has the function for removing data in set. It is also using an example with the consideration of continuing it from the previous example :
>>> set_example.remove(1); >>> print(set_example); {2, 3, 4, 5, 6, 7} >>> set_example.remove(5); >>> print(set_example); {2, 3, 4, 6, 7} >>> set_example.remove(7); >>> print(set_example); {2, 3, 4, 6} >>>
Replacing Data in Set
The last one is replacing the data. Unfortunately, since it does not have any index or any reference to be able to pinpoint the data, it is not possible to replace the data directly. Another way which may be a data replacement for a consideration is just by re-declaring or redefining it back. The example is also using the continuation from the above sample. Below is an example for achieving it :
>>> set_example = {5,5,5,6,7,8,9} >>> print(set_example); {5, 6, 7, 8, 9}
So, instead having the data with the number of 2, 3, 4 and 6. It has been fully replaced with a new data from the re-declaration or the redefinition of the set above.