Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Initialize dictionary with common value

Python | Initialize dictionary with common value

While working with Python, sometimes, we might have a problem in which we require the initialize the static list into a dictionary with a constant value. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using dict() + list comprehension The combination of above functions can be used to perform this particular task. In this, we just convert the elements extracted from list as keys and assign the common value using list comprehension and conversion done by dict(). 

Python3




# Python3 code to demonstrate working of
# Initialize dictionary with common value
# Using list comprehension + dict()
 
# Initialize list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initialize dictionary with common value
# Using list comprehension + dict()
res = dict((sub, 4) for sub in test_list)
 
# printing result
print("The constructed dictionary with common value : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best']
The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}

Time Complexity: O(n), where n is the length of the list test_dict
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 fromkeys() The inbuilt function of fromkeys() can also be used to perform this particular task itself and is more Pythonic way to perform this task. 

Python3




# Python3 code to demonstrate working of
# Initialize dictionary with common value
# Using fromkeys()
 
# Initialize list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initialize dictionary with common value
# Using fromkeys()
res = dict.fromkeys(test_list, 4)
 
# printing result
print("The constructed dictionary with common value : " + str(res))


Output : 

The original list is : ['gfg', 'is', 'best']
The constructed dictionary with common value : {'is': 4, 'gfg': 4, 'best': 4}

Method #3: Use the zip() function with a list comprehension

In this method, the zip() function is used to combine each element of the test_list with the value 4 using a list comprehension. The resulting pairs are then used to create a dictionary.

Python3




test_list = ['gfg', 'is', 'best']
 
# using zip() function with list comprehension
res = dict(zip(test_list, [4] * len(test_list)))
 
# printing result
print("The constructed dictionary with common value : " + str(res))


Output

The constructed dictionary with common value : {'gfg': 4, 'is': 4, 'best': 4}

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(n), as it creates a new list of length n containing the constant value 4, and a dictionary of length n to store the resulting pair

Method #4: Using defaultdict from collections

Step-by-step approach:

  • Import defaultdict from the collections module:
  • Initialize list
  • Use defaultdict with the common value as the default value to create the dictionary:
  • The update() method is used to add the keys from test_list to the defaultdict so that all keys have the common value of 4.
  • Print the result

Below is the implementation of the above approach:

Python3




from collections import defaultdict
 
# Initialize list
test_list = ['gfg', 'is', 'best']
 
# Initialize defaultdict with common value
res = defaultdict(lambda: 4)
res.update({key: 4 for key in test_list})
 
# Print the result
print("The original list is : ", test_list)
print("The constructed dictionary with common value : ", dict(res))


Output

The original list is :  ['gfg', 'is', 'best']
The constructed dictionary with common value :  {'gfg': 4, 'is': 4, 'best': 4}

Time complexity: O(n) where n is the length of test_list.
Auxiliary space: O(n) to store the dictionary.

Method #5: Using dictionary comprehension:

Step-by-step approach:

  • Initializes a list named ‘test_list’ with three string elements: ‘gfg’, ‘is’, and ‘best’.
  • Prints the original list using the ‘print()’ function and string concatenation.
  • Use a dictionary comprehension to create a new dictionary named ‘res’. The dictionary comprehension iterates through each element (sub) in ‘test_list’ and creates a key-value pair where the key is the current element (sub) and the value is 4.
  • Finally, prints the resulting dictionary using the ‘print()’ function and string concatenation.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Initialize dictionary with common value
# Using dictionary comprehension
 
# Initialize list
test_list = ['gfg', 'is', 'best']
 
# printing original list
print("The original list is: " + str(test_list))
 
# Initialize dictionary with common value
# Using dictionary comprehension
res = {sub: 4 for sub in test_list}
 
# printing result
print("The constructed dictionary with common value: " + str(res))


Output

The original list is: ['gfg', 'is', 'best']
The constructed dictionary with common value: {'gfg': 4, 'is': 4, 'best': 4}

Time complexity: O(n), where n is the number of elements in the list. The dictionary comprehension iterates through each element in the list once.
Auxiliary space: O(n), where n is the number of elements in the list. The dictionary created has n key-value pairs, where n is the length of the list.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments