Open In App

Python – Convert list to Single Dictionary Key Value list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Convert list to Single Dictionary Key Value list
# Using loop
 
# initializing list
test_list = [5, 6, 3, 8, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Convert list to Single Dictionary Key Value list
# Using loop
res = {test_list[K]: []}
for idx in range(len(test_list)):
    if idx != K:
        res[test_list[K]].append(test_list[idx])
 
# printing result
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




# Python3 code to demonstrate working of
# Convert list to Single Dictionary Key Value list
# Using loop
 
# initializing list
test_list = [5, 6, 3, 8, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# Convert list to Single Dictionary Key Value list
# Using loop
res = {test_list[K]: test_list[:K] + test_list[K + 1:]}
 
# printing result
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




# defining the input list
test_list = [5, 6, 3, 8, 9]
 
# defining the index of the key in the dictionary
K = 3
 
# using dictionary comprehension to create the dictionary
# iterating over the list using enumerate() to access both index and value of each element
# adding each element to the dictionary value list if its index is not equal to K
res = {test_list[K]: [x for i, x in enumerate(test_list) if i != K]}
 
# printing the result
print(res)


Output

{8: [5, 6, 3, 9]}

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




# defining the input list
test_list = [5, 6, 3, 8, 9]
 
# defining the index of the key in the list
K = 3
 
# using list comprehension to create a new list with all elements except the one at index K
new_list = [x for i, x in enumerate(test_list) if i != K]
 
# creating a dictionary with the key at index K and the new list as its value
res = {test_list[K]: new_list}
 
# printing the result
print(res)


Output

{8: [5, 6, 3, 9]}

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:

  1. Initialize the original list test_list and the index K.
  2. Create a set keys with the element at index K in test_list.
  3. Create another set values with all the elements in test_list except the element at index K.
  4. Subtract keys from values to get a set of all the values that are not at index K.
  5. Create a new dictionary res with the key as the element in keys and the value as a list of the elements in values.
  6. Print the original list and the resulting dictionary.

Python3




# initializing list
test_list = [5, 6, 3, 8, 9]
 
# initializing K
K = 3
 
# Convert list to Single Dictionary Key Value list
# Using set operations
keys = {test_list[K]}
values = set(test_list) - keys
res = {keys.pop(): list(values)}
 
# printing original list and result
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

  1. Create a list test_list with values [5, 6, 3, 8, 9] and a variable K with a value of 3.
  2. 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.
  3. Create a dictionary called res with the key key and the value test_list. The dictionary will look like {8: [5, 6, 3, 9]}.
  4. Print the original list test_list which will have the values [5, 6, 3, 9].
  5. Print the dictionary res which will have the key 8 and the value [5, 6, 3, 9]. 

Python3




# Initializing list and value
test_list = [5, 6, 3, 8, 9]
K = 3
 
# Popping element in list
key = test_list.pop(K)
res = {key: test_list}
 
print("The original list is: ", test_list)
 
# Printing the result
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
Previous
Next
Share your thoughts in the comments
Similar Reads