Friday, December 27, 2024
Google search engine
HomeLanguagesPython program to convert Set into Tuple and Tuple into Set

Python program to convert Set into Tuple and Tuple into Set

Let’s see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type().

  • tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.
  • set(): set method is to convert other type values to set this method is also accepted other type values as an argument and return a set type value.
  • type(): type method helps the programmer to check the data type of value. This method accepts a value as an argument and it returns type of the value.

Example:

Input: {'a', 'b', 'c', 'd', 'e'}
Output: ('a', 'c', 'b', 'e', 'd')
Explanation: converting Set to tuple

Input: ('x', 'y', 'z')
Output: {'z', 'x', 'y'}
Explanation: Converting tuple to set

Example 1: convert set into tuple.

Python




#   program to convert set to tuple
# create set
s = {'a', 'b', 'c', 'd', 'e'}
 
# print set
print(type(s), " ", s)
 
# call tuple() method
# this method convert set to tuple
t = tuple(s)
 
# print tuple
print(type(t), " ", t)


Output

(<type 'set'>, ' ', set(['a', 'c', 'b', 'e', 'd']))
(<type 'tuple'>, ' ', ('a', 'c', 'b', 'e', 'd'))

Time complexity: O(n), where n is the number of elements in the set.
Auxiliary space: O(n), where n is the number of elements in the set, due to the creation of a new tuple.

Method2: Using list comprehension

Python3




s = {'a', 'b', 'c', 'd', 'e'}
x=[i for i in s]
print(tuple(x))


Output

('a', 'd', 'b', 'c', 'e')

Time complexity: O(n), where n is the size of the set s.
Auxiliary space: O(n), as we are creating a list x with n elements, where n is the size of the set s

Method #3: Using enumerate function

Python3




s = {'a', 'b', 'c', 'd', 'e'}
x=[i for a,i in enumerate(s)]
print(tuple(x))


Output

('e', 'a', 'd', 'b', 'c')

The time complexity of the program is O(n), where n is the size of the set s. 

The auxiliary space of the program is also O(n), where n is the size of the set s. 

Example #2: tuple into the set.

Step-by-step approach:

  • Create a tuple t with three elements: ‘x’, ‘y’, and ‘z’.
  • Print the type of t and its contents using print(type(t), ” “, t).
  • Call the set() method with t as its argument. This creates a new set s with the same elements as t, but in an arbitrary order.
  • Print the type of s and its contents using print(type(s), ” “, s). The output will show that s is a set containing ‘x’, ‘y’, and ‘z’.

Below is the implementation of the above approach:

Python




#program to convert tuple into set
 
# create tuple
t = ('x', 'y', 'z')
 
# print tuple
print(type(t), "  ", t)
 
# call set() method
s = set(t)
 
# print set
print(type(s), "  ", s)


Output

(<type 'tuple'>, '  ', ('x', 'y', 'z'))
(<type 'set'>, '  ', set(['y', 'x', 'z']))

Time complexity: O(n), where n is the size of the input tuple. The reason being, the program iterates through each element of the tuple once to convert it into a set.
Auxiliary space: O(n) as well, where n is the size of the input tuple. The reason being, the program creates a new set containing all elements of the tuple, which requires additional memory space equivalent to the size of the input tuple.

Method #4: Using the ‘*’ operator the ‘*’ operator can be used to unpack the elements of a set into a tuple.

Python3




#initializing a set
test_set = {6, 3, 7, 1, 2, 4}
 
#converting the set into a tuple
test_tuple = (*test_set,)
 
#printing the converted tuple
print("The converted tuple : " + str(test_tuple))


Output

The converted tuple : (1, 2, 3, 4, 6, 7)

Algorithm:

  1. Initialize a set with some elements.
  2. Convert the set to a tuple using the unpacking operator (*).
  3. Print the converted tuple.

Time Complexity: The time complexity of the code is O(n) because converting a set to a tuple, using the unpacking operator (*), takes linear time.
Auxiliary Space: The auxiliary space complexity of the code is O(n) because the tuple size is proportional to the set size.

Method #3: Using the += operator

We initialize an empty tuple t. Then we loop through each element i in the set s. For each element, we create a one-element tuple (i,) and concatenate it to the existing tuple t using the += operator. Finally, we print the resulting tuple t. This approach does not use list comprehension and instead uses a loop and tuple concatenation to create the final tuple.

Python3




s = {'a', 'b', 'c', 'd', 'e'}
 
# initialize an empty tuple
t = tuple()
 
# loop through each element in the set s
for i in s:
    # create a one-element tuple with the current element i and concatenate it to t
    t += (i,)
 
# print the resulting tuple
print(t)


Output

('a', 'c', 'd', 'e', 'b')

Time Complexity: O(n) because converting a set to a tuple, using the unpacking operator (*), takes linear time.
Auxiliary Space: O(n) because the tuple size is proportional to the set size.

Method #4:Using itertools library’s tuple() function.
Algorithm:

  1. Import the itertools module
  2. Initialize a set s with elements ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’
  3. Initialize an empty tuple t
  4. Use the itertools.chain() function to chain the elements of s and return a single iterable object
  5. Use the tuple() function to convert the iterable object to a tuple
  6. Assign the resulting tuple to variable t
  7. Print the tuple t

Python3




import itertools
#initializing set
s = {'a', 'b', 'c', 'd', 'e'}
t = tuple(itertools.chain(s))
# print the resulting tuple
 
print(t)


Output

('b', 'a', 'e', 'd', 'c')

Time Complexity:
The time complexity of the chain() function in the itertools module is O(n), where n is the total number of elements in the set s. 

Auxiliary Space: 

The Auxiliary Space of the code is O(n), where n is the number of elements in the set s.

Method #5:  Using reduce():

Algorithm:

  1. Import the reduce function from the functools module.
  2. Create a set s containing the characters ‘a’, ‘b’, ‘c’, ‘d’, ‘e’.
  3. Call the reduce function with three arguments: a lambda function, the set s, and an empty tuple as the initial value.
  4. The lambda function concatenates the current element x to the accumulator acc and returns the resulting tuple.
  5. The reduce function applies the lambda function to each element in s, updating the accumulator with the result of each step.
  6. The final value of the accumulator is the resulting tuple t and print the resulting tuple t.

Below is the implementation of the above approach:

Python3




# Python program for the above approach
from functools import redu
 
s = {'a', 'b', 'c', 'd', 'e'}
t = reduce(lambda acc, x: acc + (x,), s, ())
 
# Print the converted tuple
print("The converted tuple : " + str(t))


Output

The converted tuple : ('e', 'c', 'b', 'd', 'a')

Time Complexity:

The time complexity of this code is O(n), where n is the number of elements in the set s. This is because the reduce function processes each element in s exactly once, and each operation takes constant time

Space Complexity:

The space complexity of this code is O(n), where n is the number of elements in the set s. This is because the reduce function creates a new tuple for each element in s, so the total space used is proportional to n. However, since tuples are immutable in Python, this does not create any significant memory overhead. The lambda function and the initial empty tuple also take constant space, so they do not contribute to space complexity.

RELATED ARTICLES

Most Popular

Recent Comments