Python set() Function
Returns a new set object which can either be an uninitialized set or from a sequence, collection, or iterator object.
Syntax
Python
Copy Code
set(iterable)
Parameters
Parameter | Description |
---|---|
iterable |
Required. The iterable object containing the values to be converted to a set. |
Example
Python
Copy Code
emptySet = set() print(emptySet) rangeSet = set(range(10, 15)) print(rangeSet) dictionary = {'apples': 5, 'oranges': 3} print(set(dictionary))
Output
set() {10, 11, 12, 13, 14} {'oranges', 'apples'}
Notes
The set()
function will return the unique items in the list.
Also note that the items in the set are unordered so they will appear in random
order.
Example
Python
Copy Code
listWithDuplicate = ['Spring', 'Summer', 'Fall', 'Winter', 'Spring'] print(set(listWithDuplicate))
Output
{'Fall', 'Winter', 'Spring', 'Summer'}