π¬ **Problem Formulation**: Given a Python set. How to convert it to a tuple? And how to convert the tuple back to a set?

There are three main ways:

**Method 1**: To convert a Python**set to a tuple**, use the`tuple(my_set)`

function.**Method 2**: To convert a Python**tuple to a set**, use the`set(my_tuple)`

function.**Method 3**: To convert a Python tuple of mutable elements to a set, use the expression`set(tuple(x) for x in my_tuple)`

to avoid a`TypeError`

.

I’ll also give you a **bonus method 4 **that shows you what to do to retain the **ordering information** when converting a tuple to a set—so keep reading! π

Table of Contents

## Method 1: Convert Set to Tuple with tuple()

To convert a set to a tuple, pass the set into the `tuple()`

function. This is a built-in Python function, so you don’t need to import or install any library to use it. The return value is a new tuple from the values in the set.

π Here’s an example where you convert the set `{1, 2, 3}`

to a tuple `(1, 2, 3)`

:

my_set = {1, 2, 3} my_tuple = tuple(my_set) print(my_tuple) # (1, 2, 3)

By the way, here’s an explainer video on this function:

## Method 2: Convert Tuple to Set with set()

To convert a tuple to a set, pass the tuple into the `set()`

function. This is a built-in Python function, so you don’t need to import or install any library. The return value is a new set from the values in the tuple.

π Here’s an example where you convert the tuple `(1, 2, 3)`

to a set `{1, 2, 3}`

:

my_tuple = (1, 2, 3) my_set = set(my_tuple) print(my_set) # {1, 2, 3}

β‘ **Problem**: However, the conversion process from a tuple to set doesn’t always work because if you try to convert a tuple of mutable values, Python will raise the `TypeError: unhashable type`

!

Read on to learn more about this problem—and how to resolve it easily: π

## Method 3: Convert Tuple to Set with Set Comprehension

π‘ A **Python set** is an **unordered collection of unique immutable elements**. Each element must define explicitly or implicitly the `__hash__()`

dunder method, i.e., must be hashable.

If you attempt to convert a tuple of mutable elements (e.g., lists) to a set, Python will raise an error such as the `TypeError: unhashable type: 'list'`

:

my_tuple = ([1, 2], [3, 4], [5, 6]) my_set = set(my_tuple) # TypeError: unhashable type: 'list'

In this case, you can use set comprehension to convert each inner tuple element to an immutable type. For example, the expression `set(tuple(x) for x in my_tuple)`

converts each inner list to a tuple. The result is a set of immutable tuples.

Here’s the solution to this problem in a minimal code example:

my_tuple = ([1, 2], [3, 4], [5, 6]) my_set = set(tuple(x) for x in my_tuple) print(my_set) # {(1, 2), (3, 4), (5, 6)}

The final “bonus” section introduces another elegant way to retain the ordering information in a set:

## Bonus Method 4: Enumerate Elements

Sometimes, you want to associate each set or tuple element with a specific numerical “index” value, i.e., a unique integer identifier. The `enumerate()`

method to the rescue!

- Use
`tuple(enumerate(my_set))`

to convert a set to an enumerated tuple. - Use
`set(enumerate(my_tuple))`

to convert a tuple to an enumerated set.

The result is the respective container data structure with (identifier, value) tuples:

my_set = {'Alice', 'Bob', 'Carl'} my_tuple = tuple(enumerate(my_set)) print(my_tuple) # ((0, 'Carl'), (1, 'Bob'), (2, 'Alice')) my_set = set(enumerate(('Alice', 'Bob', 'Carl'))) print(my_set) # {(2, 'Carl'), (0, 'Alice'), (1, 'Bob')}

Especially in the case where you convert a tuple to a set, this makes a lot of sense because you can retain the information on the ordering of elements that would be otherwise lost after converting to a set.

Thanks for reading the whole article, my friend! You can join us here (we have cheat sheets too):

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.