Python | Convert List of Dictionary to Tuple list

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

Resultant list of tuples: [('b', 4, 5, 6), ('a', 1, 2, 3), ('d', 10, 11, 12), ('c', 7, 8, 9)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.