# Python program to convert Set into Tuple and Tuple into Set

PythonServer Side ProgrammingProgramming

When it is required to convert a set structure into a tuple, and a tuple into a set, the ‘tuple’ and ‘set’ methods can be used.

Below is a demonstration of the same −

## Example

Live Demo

my_set = {'ab', 'cd', 'ef', 'g', 'h', 's', 'v'}
print("The type is : ")
print(type(my_set), " ", my_set)
print("Converting a set into a tuple")
my_tuple = tuple(my_set)
print("The type is : ")
print(type(my_tuple), " ", my_tuple)
my_tuple = ('ab', 'cd', 'ef', 'g', 'h', 's', 'v')
print("The tuple is:")
print(my_tuple)
print(type(my_tuple), " ", my_tuple)
print("Converting tuple to set")
my_set = set(my_tuple)
print(type(my_set), " ", my_set)

## Output

The type is :
<class 'set'> {'ef', 'g', 'h', 's', 'ab', 'v', 'cd'}
Converting a set into a tuple
The type is :
<class 'tuple'> ('ef', 'g', 'h', 's', 'ab', 'v', 'cd')
The tuple is:
('ab', 'cd', 'ef', 'g', 'h', 's', 'v')
<class 'tuple'> ('ab', 'cd', 'ef', 'g', 'h', 's', 'v')
Converting tuple to set
<class 'set'> {'ef', 'g', 'h', 's', 'ab', 'v', 'cd'}

## Explanation

• A set is defined and is displayed on the console.

• The type of this data structure is determined using the ‘type’ method.

• It is converted to a tuple using the ‘tuple’ method.

• The type of this type is determined using the ‘type’ method.

• Now to convert this tuple back to set, the ‘set’ method is used.

• This type is determined and is displayed as the output on the console.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6660493,"math_prob":0.7413762,"size":2586,"snap":"2022-27-2022-33","text_gpt3_token_len":719,"char_repetition_ratio":0.1750581,"word_repetition_ratio":0.07855626,"special_character_ratio":0.31283835,"punctuation_ratio":0.14741036,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98954594,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-11T23:59:44Z\",\"WARC-Record-ID\":\"<urn:uuid:ec005f31-422c-4f7a-b897-a8dcdcd13374>\",\"Content-Length\":\"31578\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1ab7460d-cc09-445e-8535-f4259584fe14>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d91be67-64a6-4212-b976-fcbb2eafcd61>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/python-program-to-convert-set-into-tuple-and-tuple-into-set\",\"WARC-Payload-Digest\":\"sha1:JFLGVZUKFQQAM6VCX3UU2ULBKL4BQXTS\",\"WARC-Block-Digest\":\"sha1:WONNCKBF4JECUE3PWAGLUTGGXLYVOVP7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571536.89_warc_CC-MAIN-20220811224716-20220812014716-00139.warc.gz\"}"}