Python set() Class
The set() class in Python returns a new set object, optionally with elements taken from iterable.
Syntax
1 2 3 |
class set([iterable]) |
Parameter
iterable : This parameter represents a sequence, collection or an iterator object
Return
This function returns a new set object where the items in a set list are unordered.
Example 1
1 2 3 4 5 6 7 8 9 10 |
# Python program explaining # the set() function # initializing list list_val = [ 13, 14, 11, 14, 15 ] # Printing iterables before conversion print("The list value before conversion is : " + str(list_val)) # Printing iterables after conversion print("The list value after conversion is : " + str(set(list_val))) |
Output
1 2 3 4 |
The list value before conversion is : [13, 14, 11, 14, 15] The list value after conversion is : {11, 13, 14, 15} |
Example 2
1 2 3 4 5 6 7 8 9 10 |
# Python program explaining # the set() function # initializing string list str_list = ("apple", "banana", "cherry") # Printing iterables before conversion print("The list value before conversion is : " + str(str_list)) # Printing iterables after conversion print("The list value after conversion is : " + str(set(str_list))) |
Output
1 2 3 4 |
The list value before conversion is : ('apple', 'banana', 'cherry') The list value after conversion is : {'banana', 'apple', 'cherry'} |