Python Program to create a List using custom key-value pair of a dictionary - GeeksforGeeks

02 Feb, 2021
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 listtest_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 listprint("The original list is : " + str(test_list))  # initializing key-valueskey, value = 'gfg', 'best'  res = dict()for sub in test_list:      # constructed values    res[sub[key]] = sub[value]  # printing resultprint("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 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 listtest_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 listprint("The original list is : " + str(test_list))  # initializing key-valueskey, value = 'gfg', 'best'  # dictionary comprehension for one linerres = {sub[key]: sub[value] for sub in test_list}  # printing resultprint("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}
Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?