Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Create dictionary from the list

Python – Create dictionary from the list

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




# Python3 code to demonstrate working of
# Values derived Dictionary keys
# Using loop
 
# initializing list
test_list = ["gfg", "is", "best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "def_key_"
 
# using loop to construct new Dictionary
# + operator used to concat default key and values
res = dict()
for ele in test_list:
    res[K + str(ele)] = ele
 
# printing result
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




# Python3 code to demonstrate working of
# Values derived Dictionary keys
# Using dictionary comprehension
 
# initializing list
test_list = ["gfg", "is", "best"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = "def_key_"
 
# using dictionary comprehension
# + operator used to concat default key and values
res = {K + str(ele) : ele for ele in test_list}
 
# printing result
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"
# printing original list
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)
#This code is contributed by Jyothi pinjala


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))
 
# Method 4: Using zip and list comprehension
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.

RELATED ARTICLES

Most Popular

Recent Comments