How To Convert a List Of Tuples Into a Dictionary in Python – With Examples

Tuples are used to store multiple items in a single object.

You can convert a list of tuples into a dictionary in Python using the yourdict = dict(list_of_tuples) statement.

This tutorial teaches you the different methods to convert a list of tuples into a dictionary and when it is appropriate to use each method.

If You’re in Hurry…

You can use the dict() constructor to convert a list of tuples into a dictionary.

  • Each tuple in the list must have the same size while using the dict constructor.
list_of_tuples = [('Name', 'Ram'), ('Job', 'Expert'), ('Salary', '90000')]

yourdict = dict(list_of_tuples)

yourdict

Read details to understand how to convert tuples of different sizes into a dictionary.

Output

 {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}

If You Want to Understand Details, Read on…

The tuple is an ordered collection, and it is unchangeable. It is impossible to add or remove elements once the tuple is created. You can only access the elements.

Using Dict() constructor

You can use the dict() constructor to convert the list of tuples into a dictionary. This is the simplest method to convert tuples into a dictionary.

While using the dict() constructor, all the tuples in the list must be of the same size and have only two values.

  • The first value in the tuple will be the entry’s key.
  • The second value in the tuple will be the value of each dictionary entry.

If you pass the tuples of different sizes in the list, you’ll face ValueError.

Use this when you are sure that all the tuples in the list are the same size. And the first item in the tuple must be the key, and the second item must be the value. This is no change required in this.

Code

list_of_tuples = [('Name', 'Ram'), ('Job', 'Expert'), ('Salary', '90000')]

yourdict = dict(list_of_tuples)

yourdict

Output

 {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}

Using Dict Comprehension

The other method to convert a list of tuples into a dictionary is to use dictionary comprehension.

  • Dictionary comprehension iterates over the list of tuples
  • A tuple is returned during each iteration. From that tuple, a dictionary entry is created.
  • The first item is key, and the second item is value. Once the iterations are completed, a dictionary will be created and assigned.

While using this method,

  • Tuples need not be of the same size. You can use the index to access the item from a specific position and use it as a key or value for the dictionary.
  • You can use the item at any index as a key/value of the dictionary. But it is the same for all the tuples in the dictionary.

You can use this method when unsure about the size of the tuples and use different index items as key/value instead of the first/second item.

Code

The following code demonstrates how to use dictionary comprehension to convert a list of tuples to a dictionary.

list_of_tuples = [('Name', 'Ram'), ('Job', 'Expert'), ('Salary', '90000')]

your_dict = {t[0]: t[1] for t in list_of_tuples}

your_dict

Output

    {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}

Using Unpacking

You can use the unpack method to create a dictionary from a list of tuples.

It iterates over the list of tuples and returns the value set during each iteration. You can use the : operator to create a dictionary entry during each iteration.

While using this method,

  • Tuples must be of the same size and must have only two values.
  • You can use the first or second item as a key/ value. It is not a must to have the first item in the tuple as the key and the second item as the value.

Code

The following code demonstrates how to use the unpack method to create a dictionary from the list of tuples.

list_of_tuples = [('Name', 'Ram'), ('Job', 'Expert'), ('Salary', '90000')]

yourdict = {key: val for key, val in list_of_tuples}

yourdict

Output

    {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}

Using Second Item As Key And First Item As Value

The following code demonstrates how to use the second item as the key and the first item as the value from the tuple.

list_of_tuples = [('Name', 'Ram'), ('Job', 'Expert'), ('Salary', '90000')]

yourdict = {key: val for val, key in list_of_tuples}

yourdict

Output

    {'Ram': 'Name', 'Expert': 'Job', '90000': 'Salary'}

Converting Tuples With More than Two Elements To Dict

This section demonstrates how to convert tuples with more than two elements into a dictionary.

You have to use the dictionary comprehension method, and the index numbers to specify which index item must be used as the key and value of the dictionary entry.

If the specified index is unavailable in any of the tuples, you’ll face the ValueError.

Code

list_of_tuples = [('Name', 'Ram', 'A'), ('Job', 'Expert','Grade A'), ('Salary', '90000')]

your_dict = {t[0]: t[1] for t in list_of_tuples}

your_dict

Output

    {'Name': 'Ram', 'Job': 'Expert', 'Salary': '90000'}

Conclusion

You’ve learned how to create a dictionary using a list of tuples using different methods and when it is appropriate to use each method.

If you have any questions, please feel free to comment below.

You May Also Like

Leave a Comment