Python set()
The set() function in python helps to create a python set object.
Python set()
The syntax of set() function is:
1
2
set(iterable)
set() Parameters
set() takes only one parameter as an argument:
- iterable (optional) - an iterable object can be a sequence or collection of objects. (Like list, string tuple, dictionary etc.)
Let see some examples of the set() in python.
Example 1: How to use the set() function in python?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# empty set
print(set())
# from string
print(set('Python'))
# from tuple
print(set((1,2,3,4,5)))
# from list
print(set(['a', 'e', 'i', 'o', 'u']))
# from range
print(set(range(5)))
Output:
1
2
3
4
5
6
set()
{'y', 'P', 't', 'o', 'h', 'n'}
{1, 2, 3, 4, 5}
{'i', 'e', 'o', 'a', 'u'}
{0, 1, 2, 3, 4}
Here we are converting a string, tuple, list and range into a set using the set() function, and as the set is an unordered sequence, we are getting output according to it. Also, note that when creating an empty set, you must use the set() function and if your building an empty set using { } syntax, it will create a dictionary, not a set.
Example 2: How to use a set() function with dictionary and frozenset in python?
1
2
3
4
5
6
7
# from dictionary
print(set({'a':1, 'e': 2, 'i':3, 'o':4, 'u':5}))
# from frozen set
frozen_set = frozenset(('a', 'e', 'i', 'o', 'u'))
print(set(frozen_set))
Output
1
2
3
{'i', 'a', 'u', 'o', 'e'}
{'i', 'a', 'u', 'o', 'e'}
Rules of the set()
- A set() function will take only an iterable object as a parameter.
- An empty set can be created with no parameter if passed.
This post is licensed under CC BY 4.0 by the author.