Python – Convert list to Single Dictionary Key Value list
Sometimes, while working with Python lists, we can have a problem in which we need to convert the list into a dictionary, which has single key, the Kth element of list, and others as its list value. This kind of problem can have applications in data domains. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [6, 5, 3, 2], K = 1
Output : {5 : [6, 3, 2]}
Input : test_list = [6, 5, 3, 2], K = 2
Output : {5 : [6, 5, 2]}
Method #1: Using loop
This is one of the ways in which this task can be performed. In this, we create a key initially and then append values except K index to create a dictionary list.
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
res = {test_list[K]: []}
for idx in range ( len (test_list)):
if idx ! = K:
res[test_list[K]].append(test_list[idx])
print ( "Records after conversion : " + str (res))
|
Output :
The original list is : [5, 6, 3, 8, 9]
Records after conversion : {8: [5, 6, 3, 9]}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list slicing
This is yet another one-liner in which this task can be performed. In this, we slice out the list on Kth index and assign as values in dictionary.
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
print ( "The original list is : " + str (test_list))
K = 3
res = {test_list[K]: test_list[:K] + test_list[K + 1 :]}
print ( "Records after conversion : " + str (res))
|
Output :
The original list is : [5, 6, 3, 8, 9]
Records after conversion : {8: [5, 6, 3, 9]}
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method 3: Using Dictionary Comprehension:
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
K = 3
res = {test_list[K]: [x for i, x in enumerate (test_list) if i ! = K]}
print (res)
|
Time Complexity: O(n), where n is the size of the input list.
Auxiliary Space: O(n)
Method 4: Using list comprehension
This approach first creates a new list new_list that contains all elements of the input list except the one at index K. Then, it creates a dictionary with the key at index K and the new_list as its value.
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
K = 3
new_list = [x for i, x in enumerate (test_list) if i ! = K]
res = {test_list[K]: new_list}
print (res)
|
Time complexity: O(n), where n is the length of the input list test_list. This is because we need to iterate over the input list once to create the new list new_list using a list comprehension, which takes O(n) time.
Auxiliary space: O(n) because we create a new list new_list which has n-1 elements, and a dictionary with one key-value pair. Therefore, the space required is proportional to the size of the input list.
Method 5: Using filter and lambda function
This method creates a lambda function to check if each element in the list is not equal to the element at index K, and uses the filter() function to return a list with only the elements that pass the lambda function condition. The resulting list is then used to create the final dictionary.
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
K = 3
res = {test_list[K]: list ( filter ( lambda x: x ! = test_list[K], test_list))}
print ( "Records after conversion : " + str (res))
|
Output
Records after conversion : {8: [5, 6, 3, 9]}
The time complexity of this code is O(n), where n is the length of the input list test_list.
The auxiliary space complexity of this code is also O(n), since the filter function creates a new list that contains all the elements in the original list except for the element at index K.
Method 6: Using set operations
Approach:
- Initialize the original list test_list and the index K.
- Create a set keys with the element at index K in test_list.
- Create another set values with all the elements in test_list except the element at index K.
- Subtract keys from values to get a set of all the values that are not at index K.
- Create a new dictionary res with the key as the element in keys and the value as a list of the elements in values.
- Print the original list and the resulting dictionary.
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
K = 3
keys = {test_list[K]}
values = set (test_list) - keys
res = {keys.pop(): list (values)}
print ( "The original list is: " , test_list)
print ( "Records after conversion: " , res)
|
Output
The original list is: [5, 6, 3, 8, 9]
Records after conversion: {8: [9, 3, 5, 6]}
Time complexity: O(n) where n is the length of test_list.
Auxiliary space: O(n) where n is the length of test_list since we are creating sets to store the keys and values.
Method 7 : using the pop() method
- Create a list test_list with values [5, 6, 3, 8, 9] and a variable K with a value of 3.
- Remove the element at index K from test_list using the pop() method and store its value in a variable called key. Now test_list will have the values [5, 6, 3, 9] and key will have the value 8.
- Create a dictionary called res with the key key and the value test_list. The dictionary will look like {8: [5, 6, 3, 9]}.
- Print the original list test_list which will have the values [5, 6, 3, 9].
- Print the dictionary res which will have the key 8 and the value [5, 6, 3, 9].
Python3
test_list = [ 5 , 6 , 3 , 8 , 9 ]
K = 3
key = test_list.pop(K)
res = {key: test_list}
print ( "The original list is: " , test_list)
print ( "Records after conversion: " , res)
|
Output
The original list is: [5, 6, 3, 9]
Records after conversion: {8: [5, 6, 3, 9]}
Time complexity: O(n), where n is the length of the original list, since it involves a single loop through the list to extract the element at position K.
Auxiliary space: O(n), since it involves creating a dictionary with a single key-value pair and a list with n-1 elements.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...