Python provides the set data structure which is derived from the mathematical set. The main point of the Python set is every item in a set is unique and can not be stored multiple times. This feature is the main of the Mathematical sets too. The Python set is a collection of items where they are unordered and unindexed.
Create A Set
The Python Set can be created in different ways but the most basic and popular way is using curly brackets in order to put items into the set. In the following example, we create a set named names
. As we can see that all set items are string types.
names = { "ismail" , "ahmet" , "elif" }
In the following example we create a set with composite types like string, integer etc.
person = { "ismail" , "baydan" , 37 }
The set can be also created by using the set()
constructor. The items are provided as parameters to the set() constructor method.
names = set(("ismail" , "ahmet" , "elif"))
Python Set Attributes
Before starting to work with Python set we should explain some set attributes. These attributes makes the set unique and very different from other Python data collection types like list, dictionary etc.
Unordered
The Python set items are unordered. So they do not stored according to an order which can be numerical, alpabetic, size etc. The items are represented and appeared in different order in every time the set is used or printed.
Unchangeable
Sets are unchangeable or immuteable where allreadt created sets can not be changed or edited like updating or removing. But new items can be added into the existing set.
Unique (No Duplicate)
Python set items are unique where no duplicate items can be stored in the same set. Even new item is added which is allready exist in the set there will be single item with the same value.
Add New Item To Set
The set provides the add()
method in order to new items to the existing set. The item we want to add is provided as parameter to the add() method. In the following example we add new item ali
to the set named names
.
names = { "ismail" , "ahmet" , "elif" }
names.add("ali")
print(names)
{'ismail', 'elif', 'ali', 'ahmet'}
We can see that the output contains newly added “ali” in a unordered way.
Remove Item From Set
The remove()
function can be used to remove existing item from the set. The item is provided as parameter to the remove() function. In the following example we remove the item “ali” from the set named names
.
names = { "ismail" , "ahmet" , "elif" , "ali" }
names.remove("ali")
print(names)
{'ismail', 'elif', 'ahmet'}
Get Length of Set
The set may contains single or more items. These item count can be returned by using the len()
method where the set is provided as parameter. Also the item count of the set is called as length of set. In the following example we print the item count of the set named names
.
names = { "ismail" , "ahmet" , "elif" , "ali" }
len(names)
4
Check If Given Variable Is Set
Set type is used in different cases. But there are different types in Python which are similar to the set. The list is similar to the set type. We can check if the specified variable is a set or not by using the type()
method.
names = { "ismail" , "ahmet" , "elif" }
print(type(names))