Python program to convert a list of tuples into dictionary

Python | Convert a list of Tuples into Dictionary

Sometimes you might need to convert a tuple to dict object to make it more readable.
In this article, we will try to learn how to convert a list of tuples into a dictionary. Here we will find two methods of doing this.
Examples:

Input : [("akash", 10), ("gaurav", 12), ("anand", 14), ("suraj", 20), ("akhil", 25), ("ashish", 30)] Output : {'akash': [10], 'gaurav': [12], 'anand': [14], 'ashish': [30], 'akhil': [25], 'suraj': [20]} Input : [('A', 1), ('B', 2), ('C', 3)] Output : {'B': [2], 'A': [1], 'C': [3]} Input : [("Nakul",93), ("Shivansh",45), ("Samved",65), ("Yash",88), ("Vidit",70), ("Pradeep",52)] Output : {'Nakul': [93], 'Shivansh': [45], 'Samved': [65], 'Yash': [88], 'Vidit': [70], 'Pradeep': [52]} Input : [('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)] Output : {'Sachin': 10, 'MSD': 7, 'Kohli': 18, 'Rohit': 45}

Python Exercise: Converting a list of tuples into a dictionary

Last update on March 15 2021 07:53:26 (UTC/GMT +8 hours)

List of tuples to dictionary conversion in Python

PythonServer Side ProgrammingProgramming

In this article, we are going to learn how to convert the list of tuples into a dictionary. Converting a list of tuples into a dictionary is a straightforward thing.

Follow the below steps to complete the code.

  • Initialize the list with tuples.
  • Use the dict to convert given list of tuples into a dictionary.
  • Print the resultant dictionary.

How to Convert Python Tuple to Dictionary

The dict() function in Python creates a dictionary. A dictionary is an iterable collection object that is unordered, changeable, and indexed. Python tuple() is an inbuilt function that is used to create a tuple. A tuple is an immutable sequence type.

Converting one data type to another is a common operation, and in this tutorial, we will see how to convert Python tuple to dictionary. We will also see how to convert a list of tuples into the dictionary.

Video liên quan

Postingan terbaru

LIHAT SEMUA