Python set() Function with Examples

set() Function in Python:

In Python, the set() function creates a set.

Note: set doesn’t allow duplicate elements and arranges in random order.

Syntax:

set(iterable)

Parameters

set() accepts only one optional parameter:

iterable: a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set.

Return Value:

The set() function returns:

  • If no parameters are passed, an empty set is returned.
  • Otherwise, a set constructed from the given iterable parameter is returned.

Examples:

Example1:

Input:

Given tuple = (3, 4, 3, 3, 2, 1, 1)
Given string = "btechgeeks"
Given list = [10, 20, 30, 10, 20, 40]

Output:

The result after applying set() function on the given tuple =  {1, 2, 3, 4}
The result after applying set() function on the given string =  {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}
The result after applying set() function on the given list =  {40, 10, 20, 30}

Example2:

Input:

Given tuple = (25, 35, 45)
Given string = "good"
Given list = [1, 3, 5, 6, 1]

Output:

The result after applying set() function on the given tuple =  {25, 35, 45}
The result after applying set() function on the given string =  {'o', 'g', 'd'}
The result after applying set() function on the given list =  {1, 3, 5, 6}

set() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Give the string as static input and store it in another variable.
  • Give the list as static input and store it in another variable.
  • Pass the given tuple as an argument to the set() function that converts a given tuple to a set and returns a set.
  • Store it in another variable.
  • Print the result list after applying the set() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gvn_tupl = (3, 4, 3, 3, 2, 1, 1)
# Give the string as static input and store it in another variable.
gvn_str = "btechgeeks"
# Give the list as static input and store it in another variable.
gvn_list = [10, 20, 30, 10, 20, 40]
# Pass the given tuple as an argument to the set() function that converts a given
# tuple to a set and returns a set.
# Store it in another variable.
rslt1 = set(gvn_tupl)
# Print the result list after applying set() function on the given tuple.
print("The result after applying set() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = set(gvn_str)
print("The result after applying set() function on the given string = ", rslt2)
rslt3 = set(gvn_list)
print("The result after applying set() function on the given list = ", rslt3)

Output:

The result after applying set() function on the given tuple =  {1, 2, 3, 4}
The result after applying set() function on the given string =  {'b', 'k', 't', 'e', 'g', 'c', 's', 'h'}
The result after applying set() function on the given list =  {40, 10, 20, 30}

Tip: Similarly, you can also try for the set, dictionaries.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the tuple as user input using tuple (),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the string as user input using the input() function and store it in another variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given tuple as an argument to the set() function that converts a given tuple to a set and returns a set.
  • Store it in another variable.
  • Print the result list after applying the set() function on the given tuple.
  • Similarly, do the same for the given string and list and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(int, input(
   'Enter some random Tuple Elements separated by spaces = ').split()))
# Give the string as user input using the input() function and 
# store it in another variable.
gvn_str = input("Enter some random string = ")
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given tuple as an argument to the set() function that converts a given
# tuple to a set and returns a set.
# Store it in another variable.
rslt1 = set(gvn_tupl)
# Print the result list after applying set() function on the given tuple.
print("The result after applying set() function on the given tuple = ", rslt1)
# similarly do the same for the given string and list and print it.
rslt2 = set(gvn_str)
print("The result after applying set() function on the given string = ", rslt2)
rslt3 = set(gvn_lst)
print("The result after applying set() function on the given list = ", rslt3)

Output:

Enter some random Tuple Elements separated by spaces = 3 4 2
Enter some random string = good
Enter some random List Elements separated by spaces = 6 6 7 8 8
The result after applying set() function on the given tuple = {2, 3, 4}
The result after applying set() function on the given string = {'d', 'g', 'o'}
The result after applying set() function on the given list = {8, 6, 7}