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.

  1. Python3 code to demonstrate the working of set() on list and tuple
  2. initializing list

lis1 = [ 3, 4, 1, 4, 5 ]

  1. initializing tuple

tup1 = (3, 4, 1, 4, 5)

  1. Printing iterables before conversion

print("The list before conversion is : " + str(lis1))

print("The tuple before conversion is : " + str(tup1))

  1. 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}

⚠️ **GitHub.com Fallback** ⚠️