Given a dictionary list, the task here is to write a python program that can convert it to a dictionary with items from values of custom keys.
Input : test_list = [{‘gfg’ : 1, ‘is’ : 4, ‘best’ : 6},
{‘gfg’ : 10, ‘is’ : 3, ‘best’ : 7},
{‘gfg’ : 9, ‘is’ : 4, ‘best’ : 2},
{‘gfg’ : 4, ‘is’ : 1, ‘best’ : 0},
{‘gfg’ : 6, ‘is’ : 3, ‘best’ : 8}], key, value = ‘gfg’, ‘best’
Output : {1: 6, 10: 7, 9: 2, 4: 0, 6: 8}
Explanation : Dictionary with ‘gfg”s keys and ‘best”s values is constructed.
Input : test_list = [{‘gfg’ : 1, ‘is’ : 4, ‘best’ : 6},
{‘gfg’ : 10, ‘is’ : 3, ‘best’ : 7},
{‘gfg’ : 9, ‘is’ : 4, ‘best’ : 2}], key, value = ‘gfg’, ‘best’
Output : {1: 6, 10: 7, 9: 2}
Explanation : Dictionary with ‘gfg”s keys and ‘best”s values is constructed.
Method 1 : Using loop
In this, dictionary list is iterated and values of required custom keys are extracted to declare key value pairs of result dictionary.
Example:
Python3
# initializing list test_list = [{ 'gfg' : 1 , 'is' : 4 , 'best' : 6 }, { 'gfg' : 10 , 'is' : 3 , 'best' : 7 }, { 'gfg' : 9 , 'is' : 4 , 'best' : 2 }, { 'gfg' : 4 , 'is' : 1 , 'best' : 0 }, { 'gfg' : 6 , 'is' : 3 , 'best' : 8 }] # printing original list print ( "The original list is : " + str (test_list)) # initializing key-values key, value = 'gfg' , 'best' res = dict () for sub in test_list: # constructed values res[sub[key]] = sub[value] # printing result print ( "Dictionary values : " + str (res)) |
Output:
The original list is : [{‘gfg’: 1, ‘is’: 4, ‘best’: 6}, {‘gfg’: 10, ‘is’: 3, ‘best’: 7}, {‘gfg’: 9, ‘is’: 4, ‘best’: 2}, {‘gfg’: 4, ‘is’: 1, ‘best’: 0}, {‘gfg’: 6, ‘is’: 3, ‘best’: 8}]
Dictionary values : {1: 6, 10: 7, 9: 2, 4: 0, 6: 8}
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method 2 : Using dictionary comprehension
In this, we perform similar task as above method, difference being dictionary comprehension is used to offer one liner alternative to solution.
Example:
Python3
# initializing list test_list = [{ 'gfg' : 1 , 'is' : 4 , 'best' : 6 }, { 'gfg' : 10 , 'is' : 3 , 'best' : 7 }, { 'gfg' : 9 , 'is' : 4 , 'best' : 2 }, { 'gfg' : 4 , 'is' : 1 , 'best' : 0 }, { 'gfg' : 6 , 'is' : 3 , 'best' : 8 }] # printing original list print ( "The original list is : " + str (test_list)) # initializing key-values key, value = 'gfg' , 'best' # dictionary comprehension for one liner res = {sub[key]: sub[value] for sub in test_list} # printing result print ( "Dictionary values : " + str (res)) |
Output:
The original list is : [{‘gfg’: 1, ‘is’: 4, ‘best’: 6}, {‘gfg’: 10, ‘is’: 3, ‘best’: 7}, {‘gfg’: 9, ‘is’: 4, ‘best’: 2}, {‘gfg’: 4, ‘is’: 1, ‘best’: 0}, {‘gfg’: 6, ‘is’: 3, ‘best’: 8}]
Dictionary values : {1: 6, 10: 7, 9: 2, 4: 0, 6: 8}
method 3: Using List Comprehension and Dictionary Mapping
Approach
uses a dictionary comprehension to create the desired dictionary from the input list. The dictionary comprehension iterates over each element in the input list and maps the value associated with the key to the specified value.
Algorithm
1. Initialize an empty dictionary result_dict.
2. Loop through each element elem in the input list test_list:
3. Get the value associated with the specified key key in elem using the syntax elem[key].
Get the value associated with the specified value value in elem using the syntax elem[value].
4. Add a key-value pair to the dictionary result_dict with the key being the value associated with key and the value being the value associated with value.
5. Return the resulting dictionary result_dict.
Python3
def custom_list(test_list, key, value): return {i[key]: i[value] for i in test_list} # Testing the function test_list = [{ 'gfg' : 1 , 'is' : 4 , 'best' : 6 }, { 'gfg' : 10 , 'is' : 3 , 'best' : 7 }, { 'gfg' : 9 , 'is' : 4 , 'best' : 2 }, { 'gfg' : 4 , 'is' : 1 , 'best' : 0 }, { 'gfg' : 6 , 'is' : 3 , 'best' : 8 }] key, value = 'gfg' , 'best' print (custom_list(test_list, key, value)) # {1: 6, 10: 7, 9: 2, 4: 0, 6: 8} |
{1: 6, 10: 7, 9: 2, 4: 0, 6: 8}
Time Complexity: O(n), where n is the number of elements in the input list. This is because the list comprehension and dictionary mapping both iterate over each element in the list once.
Auxiliary Space: O(n), as we are creating a dictionary with n key-value pairs.