Given a list of dictionaries, write a python code to convert list of dictionary into list if tuples.
Examples:
Input:
[{'a':[1, 2, 3], 'b':[4, 5, 6]},
{'c':[7, 8, 9], 'd':[10, 11, 12]}]
Output:
[('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)]
Input:
[{'a':['America', 'Australia'], 'b':['Bhutan', 'Bhopal']},
{'c':['Canada', 'California'], 'd':['Denmark', 'Delhi']}]
Output:
[('a', 'America', 'Australia'), ('b', 'Bhutan', 'Bhopal'),
('c', 'Canada', 'California'), ('d', 'Denmark', 'Delhi')]
Below are various methods to convert list of dictionaries to list of tuples.
Method #1: Using Naive Approach
# Python code to demonstrate # converting list of dictionary to list of tuples # initialising list of dictionary ini_list = [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] # converting to list of tuples temp_dict = {} result = [] for ini_dict in ini_list: for key in ini_dict.keys(): if key in temp_dict: temp_dict[key] += ini_dict[key] else: temp_dict[key] = ini_dict[key] for key in temp_dict.keys(): result.append(tuple([key] + temp_dict[key])) # printing result print ("Resultant list of tuples: {}".format(result)) |
Output:
Resultant list of tuples: [('a', 1, 2, 3), ('d', 10, 11, 12), ('b', 4, 5, 6), ('c', 7, 8, 9)]
Method #2: Using list comprehension
# Python code to demonstrate # converting list of dictionary to list of tuples # initialising list of dictionary ini_list = [{'a':[1, 2, 3], 'b':[4, 5, 6]}, {'c':[7, 8, 9], 'd':[10, 11, 12]}] # converting to list of tuples dict_list = [(key, )+tuple(val) for dic in ini_list for key, val in dic.items()] # printing result print ("Resultant list of tuples: {}".format(dict_list)) |
Output:
Resultant list of tuples: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Convert Tuple Matrix to Tuple List
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Convert list of tuple into dictionary
- Python - Convert tuple list to dictionary with key from a given start value
- Python - Convert Dictionary Value list to Dictionary List
- Python | Convert tuple to adjacent pair dictionary
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Convert Nested dictionary to Mapped Tuple
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python | Dictionary to list of tuple conversion
- Python | Categorize tuple values into dictionary value list
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

