Python – Unlist Single Valued Dictionary List

Given list of dictionaries, perform the unlisting of records in which we just have 1 dictionary as record element.

Input : test_list = [{‘best’: [{‘a’: 6}], ‘Gfg’: 15}]
Output : [{‘best’: {‘a’: 6}, ‘Gfg’: 15}]
Explanation : The value list associated with ‘best’ key is changed to dicationary.

Input : test_list = [{‘Gfg’: [{‘best’ : 17}]}]
Output : [{‘Gfg’: {‘best’: 17}}]
Explanation : ‘Gfg’ key’s value changed to single dictionary.

Method #1 : Using loop + isinstance()
This is brute force way in which this task can be performed. In this, we test for the list type using isinstance() and loop is used for iterations.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Unlist Single Valued Dictionary List
# Using loop + isinstance()
  
# initializing list
test_list = [{'Gfg': 1,
            'is': [{'a': 2, 'b': 3}]},
            {'best': [{'c': 4, 'd': 5}],
             'CS': 6}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Using loop + isinstance()
for dicts in test_list:
    for key, val in dicts.items():
          
        # isinstance() is used to check for list to convert
        if isinstance(val, list):
            dicts[key] = val[0]
  
# printing result 
print("The converted Dictionary list : " + str(test_list)) 

chevron_right


Output :



The original list is : [{'Gfg': 1, 'is': [{'b': 3, 'a': 2}]}, {'CS': 6, 'best': [{'d': 5, 'c': 4}]}]
The converted Dictionary list : [{'Gfg': 1, 'is': {'b': 3, 'a': 2}}, {'CS': 6, 'best': {'d': 5, 'c': 4}}]

 

Method #2 : Using list comprehension + isinstance()
The combination of above functions can be used to solve the problem. In this, we perform similar task with short hand approach similar to above method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Unlist Single Valued Dictionary List
# Using list comprehension + isinstance()
  
# initializing list
test_list = [{'Gfg': 1,
            'is': [{'a': 2, 'b': 3}]},
            {'best': [{'c': 4, 'd': 5}],
             'CS': 6}]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Using list comprehension + isinstance()
# Similar way as above, extrating first element of list 
res = [{key: val[0] if isinstance(val, list) else val
        for key, val in sub.items()}
        for sub in test_list]
  
# printing result 
print("The converted Dictionary list : " + str(res)) 

chevron_right


Output :

The original list is : [{'Gfg': 1, 'is': [{'b': 3, 'a': 2}]}, {'CS': 6, 'best': [{'d': 5, 'c': 4}]}]
The converted Dictionary list : [{'Gfg': 1, 'is': {'b': 3, 'a': 2}}, {'CS': 6, 'best': {'d': 5, 'c': 4}}]



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.