In this tutorial, we will look at how to convert a tuple of tuples to a dictionary in Python with the help of some examples.
For instance you have a tuple containing the (name, age) tuples of some people (("Tim", 21), ("Jim", 28))
and you want to convert it to a dictionary mapping of name: age, {"Tim": 21, "Jim": 28}
How to convert a tuple to a dictionary?
You can use the Python built-it dict()
function to convert a tuple to a dictionary. Note that for this method to work the tuples inside should be in the form (key, value).
Let’s look at an example.
# tuple of tuples t = (("Jim", 21), ("Tim", 28)) # create dict d = dict(t) print(d)
Output:
{'Jim': 21, 'Tim': 28}
We get the desired dictionary from the tuple above.
What would happen if the order inside each tuple it reversed? Let’s find out.
# tuple of tuples t = ((21, "Jim"), (28, "Tim")) # create dict d = dict(t) print(d)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
{21: 'Jim', 28: 'Tim'}
Here, the resulting dictionary is of the form age: name which is not the result we are looking for.
Using dictionary comprehension
A more intuitive method to construct a dictionary from a tuple of tuples would be to use a dictionary comprehension. Here, we iterate over the items in the tuple and construct our dictionary according to our requirements.
Let’s the take the example from above where the out tuple contains (age, name) tuples and we want to construct a dictionary storing name: age key-value pairs.
# tuple of tuples t = ((21, "Jim"), (28, "Tim")) # create dict d = {name:age for (age, name) in t} print(d)
Output:
{'Jim': 21, 'Tim': 28}
Here, we get the desired result with name as the key and the corresponding age as the value in the dictionary.
This method is more verbose than the previous one but it’s gives us more control as to what values to use when creating a dictionary from a tuple of tuples.
You might also be interested in –
- Convert Dictionary to List of Tuples in Python
- Convert Tuple to a List in Python – With Examples
- Python – Convert Tuple to a String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.