Python set()
Python set() Class
The set() class in Python returns a new set object, optionally with elements taken from iterable.
Syntax
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
# 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
The list value before conversion is : [13, 14, 11, 14, 15]
The list value after conversion is : {11, 13, 14, 15}
Example 2
# 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
The list value before conversion is : ('apple', 'banana', 'cherry')
The list value after conversion is : {'banana', 'apple', 'cherry'}
