The set in Python
- By unordered, it means the position of the elements in a set is not recorded. And so, you may not perform indexing, slicing, etc. (the operations that you can perform in sequences)
- The collection in a set may not contain duplicate entries. Basically, the set is used for eliminating duplicate entries and membership testing.
- You may perform certain mathematical operations in a set as well.
Syntax of creating Python sets
This is how you may create sets, e.g.
So, in the curly braces, items are separated by commas.
You may also create set comprehension just like lists.
The set comprehension involves creating a new set where items of iterable (a sequence) go through a certain operation.
In next section, I will show you how to create simple sets along with set comprehension examples with code.
A demo of creating and displaying a set
In this example, a set of five unordered numbers is created. After that, a for loop is used to display its elements:
The code for creating and displaying the set:
# An example of constructing a set set_ex = {10,20,30,40,50} for curr_set_item in set_ex: print ("The set item:",curr_set_item)
Output:
An example of creating a set of strings
In this example, a set of Python is created for storing the names. This is how the set is created and displayed:
The code:
# An example of constructing a set name_set = {'Tina','Mike','Shaun', 'Nena','Samina'} print (name_set)
The output of the above code is:
What happens if a set contains duplicate values?
In this example, the name ‘Mike’ is used twice as creating a name set. The next statement uses the print function to display the set items
The code:
# An example of constructing a set name_set = {'Tina','Mike','Shaun', 'Nena','Samina', 'Mike'} print (name_set)
The output is the same as in above example, i.e. it removed the duplicate entry:
An example of set comprehension
- In this example, two sets are constructed.
- The first set contains five numbers.
- The other set is created based on the first set by squaring the values of each item in the first set.
The code:
set1 = {3,5,8,10,20} set_compre = {x**2 for x in set1} print ("The first set:",set1) print ("A set comprehension:",set_compre)
Output:
An example of getting the length of a set
You may use the len() method to get the total unique items in the specified set. It will not include duplicate entries.
See the example below where two sets are constructed and the length of each is displayed by the len method.
For demo purposes, the second set is given a duplicate entry.
The code:
set_numbers = {3,5,8,10,20} set_fruits = {'Apple','Banana', 'Orange','Banana'} print ("The length of first set =",len(set_numbers)) print ("The length of fruit set = ",len(set_fruits))
Output:
You can see that although the second set contains four items, its length is returned as 3. Because the word “Banana” is used twice in the second set.
An example of clearing set items
The code for removing elements:
set_fruits = {'Apple','Banana', 'Orange'} print ("The set before clear method =" ,set_fruits) set_fruits.clear() print ("The set after clear method = ",len(set_fruits))
Result:
The set after clear method = 0
An example of testing membership
In this example, a member or item of the set is tested whether it exists or not.
If a given name exists in the set, it will return true and a message will be displayed accordingly:
The code for testing membership:
# An example of constructing a set name_set = {'Tina','Mike','Shaun', 'Nena','Samina'} if ( 'Shaun' in name_set ): print ("The name exists in set!") else: print ("The given name does not exist!")
Output: