Python Program to create a List using custom key-value pair of a dictionary
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
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 }]
print ( "The original list is : " + str (test_list))
key, value = 'gfg' , 'best'
res = dict ()
for sub in test_list:
res[sub[key]] = sub[value]
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
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 }]
print ( "The original list is : " + str (test_list))
key, value = 'gfg' , 'best'
res = {sub[key]: sub[value] for sub in test_list}
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}
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))
|
Output
{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.
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...