Python – Create dictionary from the list
Last Updated :
01 May, 2023
Given a list. The task is to convert it to a dictionary with the values as the list element and keys as the concatenation of the given string K and value.
Examples:
Input : test_list = [“gfg”, “is”, “best”], K = “pref_”
Output : {‘pref_gfg’: ‘gfg’, ‘pref_is’: ‘is’, ‘pref_best’: ‘best’}
Explanation : Keys constructed after concatenating K.
Input : test_list = [“gfg”, “best”], K = “pref_”
Output : {‘pref_gfg’: ‘gfg’, ‘pref_best’: ‘best’}
Explanation : Keys constructed after concatenating K.
Method #1: Using loop
This is one of the ways in which this task can be performed. In this, we perform concatenation using + operator to construct keys and values are extracted from list.
Python3
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
K = "def_key_"
res = dict ()
for ele in test_list:
res[K + str (ele)] = ele
print ( "The constructed Dictionary : " + str (res))
|
Output:
The original list is : [‘gfg’, ‘is’, ‘best’] The constructed Dictionary : {‘def_key_gfg’: ‘gfg’, ‘def_key_is’: ‘is’, ‘def_key_best’: ‘best’}
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created
Method #2: Using dictionary comprehension
This is yet another way in which this task can be performed. In this, we construct a dictionary using one-liner using dictionary comprehension.
Python3
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
K = "def_key_"
res = {K + str (ele) : ele for ele in test_list}
print ( "The constructed Dictionary : " + str (res))
|
Output:
The original list is : [‘gfg’, ‘is’, ‘best’] The constructed Dictionary : {‘def_key_gfg’: ‘gfg’, ‘def_key_is’: ‘is’, ‘def_key_best’: ‘best’}
Method #3: Using dict.fromkeys:
Python3
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
K = "def_key_"
res = dict .fromkeys(test_list, K)
res = {key: K + key for key in res}
print ( "The constructed Dictionary : " , res)
|
Output
The original list is : ['gfg', 'is', 'best']
The constructed Dictionary : {'gfg': 'def_key_gfg', 'is': 'def_key_is', 'best': 'def_key_best'}
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 4: Use the built-in zip function along with a list comprehension.
Step-by-step approach:
- Initialize two lists: one with the keys (test_list) and another with the corresponding values (each value being the concatenation of “def_key_” and the corresponding key).
- Use the zip function to create a list of tuples where each tuple contains a key-value pair.
- Use a list comprehension to create a dictionary from the list of tuples.
- Return the resulting dictionary.
Python3
test_list = [ "gfg" , "is" , "best" ]
print ( "The original list is : " + str (test_list))
prefix = "def_key_"
keys = test_list
values = [prefix + key for key in keys]
res = dict ( zip (keys, values))
print ( "The constructed Dictionary : " , res)
|
Output
The original list is : ['gfg', 'is', 'best']
The constructed Dictionary : {'gfg': 'def_key_gfg', 'is': 'def_key_is', 'best': 'def_key_best'}
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), since we create two additional lists (keys and values) that are of the same size as the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...