![]() |
VOOZH | about |
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().
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.
(<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.
('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
('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:
Below is the implementation of the above approach:
(<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.
The converted tuple : (1, 2, 3, 4, 6, 7)
Algorithm:
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.
('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:
('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:
Below is the implementation of the above approach:
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.