Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Create a dictionary using list with none values

Python – Create a dictionary using list with none values

Sometimes you might need to convert a list to dict object for some better and fast operation. Let’s see how to convert a list into a dictionary of none values. Here we will find three methods of doing this.

Method #1: Using zip()and dict 

Python3




# Python code to demonstrate
# converting list into dictionary with none values
# using zip() and dictionary
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print ("initial list", str(ini_list))
 
# Converting list into dictionary using zip() and dictionary
res = dict(zip(ini_list, [None]*len(ini_list)))
 
# printing final result
print ("final dictionary", str(res))


Output: 

initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

 

Method #2: Using dict 

Python3




# Python code to demonstrate converting
# list into dictionary with none values
# using dict()
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print ("initial list", str(ini_list))
 
# Converting list into dict()
res = dict.fromkeys(ini_list)
 
# printing final result
print ("final dictionary", str(res))


Output: 

initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

 

Method #3: Using dict comprehension  

Python3




# Python code to demonstrate converting
# list into dictionary with none values
# using dict comprehension
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print ("initial list", str(ini_list))
 
# Converting list into dict()
res = {key: None for key in ini_list}
 
# printing final result
print ("final dictionary", str(res))


Output: 

initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

 

Method #4: using enumerate()

Python3




# Initialize a list
ini_list = [1, 2, 3, 4, 5]
 
# Print the initial list
print("initial list", str(ini_list))
 
# Use a dictionary comprehension and the enumerate() function
# to convert the list into a dictionary with None values
res = {k: None for i, k in enumerate(ini_list)}
 
# Print the final dictionary
print("final dictionary", str(res))
#this code is contributed Vinay Pinjala.


Output

initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

Time Complexity:O(N)
Auxiliary Space :O(N)

Method #5: Using a loop and the setdefault() method of the dictionary

Steps:

  • Initialize an empty dictionary.
  • Iterate through each element of the list using a loop and for each element, and use the setdefault() method of the dictionary to add a new key-value pair with the element as the key and None as the value. If the key already exists in the dictionary, setdefault() does not add a new key-value pair and returns the existing value.
  • The result is the dictionary with the key-value pairs from the list, where all values are None.

Python3




# Python code to demonstrate converting list
# into dictionary with none values
# using a loop and setdefault()
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print ("initial list", str(ini_list))
 
# Converting list into dictionary using
# a loop and setdefault()
res = {}
for element in ini_list:
    res.setdefault(element, None)
 
# printing final result
print ("final dictionary", str(res))


Output

initial list [1, 2, 3, 4, 5]
final dictionary {1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the list ini_list.
Auxiliary space: O(n), to store the dictionary with the key-value pairs.

Method #6: Using pop() function and a while loop to create the dictionary with None values

  1. Initialize the input list, ini_list.
  2. Initialize an empty dictionary, res.
  3. Loop over the range of indices of ini_list in ascending order:
    • Use pop(0) to remove the first element from ini_list.
    • Add the removed element as a key to the res dictionary with a value of None.
  4. Print the resulting res dictionary.
     

Python3




# initialize the input list
ini_list = [1, 2, 3, 4, 5]
 
# initialize an empty dictionary
res = {}
 
# printing initialized list
print("initial list", str(ini_list))
 
# loop over the range of indices of
# ini_list in ascending order
for i in range(len(ini_list)):
    res[ini_list.pop(0)] = None
 
# printing final result
print("final dictionary", str(res))


Output

{1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the input list. 
Space complexity: O(n), where n is the length of the input list. 

Method #7: Using map and lambda functions:

  • We start by initializing a list of integers ini_list.
  • We print the initialized list using the print() function.
  • We create a new dictionary by calling map() function, which applies a lambda function to each element of the ini_list and returns a new list of tuples with each tuple consisting of an element of ini_list and None.
  • We convert the list of tuples into a dictionary by calling the dict() function on the result from step 3.
  • We print the final dictionary using the print() function.

Python3




# Python code to demonstrate
# converting list into dictionary with none values
# using map() and lambda functions
 
# initializing list
ini_list = [1, 2, 3, 4, 5]
 
# printing initialized list
print("initial list:", ini_list)
 
# Converting list into dictionary using map() and lambda functions
res = dict(map(lambda x: (x, None), ini_list))
 
# printing final result
print("final dictionary:", res)


Output

initial list: [1, 2, 3, 4, 5]
final dictionary: {1: None, 2: None, 3: None, 4: None, 5: None}

Time complexity: O(n), where n is the length of the input list. 
Space complexity: O(n), where n is the length of the input list. 

RELATED ARTICLES

Most Popular

Recent Comments