0. Python | set() method - nealtran1905/PythonForResearch GitHub Wiki
set() method is used to convert any of the iterable to the distinct element and sorted sequence of iterable elements, commonly called Set.
- Python3 code to demonstrate the working of set() on list and tuple
- initializing list
lis1 = [ 3, 4, 1, 4, 5 ]
- initializing tuple
tup1 = (3, 4, 1, 4, 5)
- Printing iterables before conversion
print("The list before conversion is : " + str(lis1))
print("The tuple before conversion is : " + str(tup1))
- Iterables after conversion are notice distinct and sorted elements
print("The list after conversion is : " + str(set(lis1)))
print("The tuple after conversion is : " + str(set(tup1)))
Output:
The list before conversion is : [3, 4, 1, 4, 5]
The tuple before conversion is : (3, 4, 1, 4, 5)
The list after conversion is : {1, 3, 4, 5}
The tuple after conversion is : {1, 3, 4, 5}