Convert list to Single Dictionary Key Value list in Python


Python is one of the most popular, high-level languages widely in use. Lists are one of the four built in data types in Python, used to store collections or groups of Data. Dictionary, Tuples and Sets are the other three data types.

When working with Python, there are often situations where you have a list of values and you need to convert it into a single dictionary with key-value pairs. This article will guide you through the process of converting a list into a dictionary in Python.

By the end, you will have a clear understanding of how to create a list and convert them to a dictionary in Python.

Approach

To convert a list into a single dictionary key-value list, we can follow the simple steps ahead −

Step 1  Let’s say we have a list, Create an empty dictionary.

Step 2  Use a loop to iterate through the list.

Step 3  Assign each element of the list as the key and its corresponding index as the value in the dictionary.

Step 4  Return the dictionary.

We will discuss all possible ways this can be handled.

Code Implementation

To create a list in Python, we can simply assign a list of values enclosed in square brackets to a variable.

List_demo = [1,2,3,4]

Now, let’s dive into the solutions −

Method 1: Using Loop

This approach involves creating a key initially and then appending values, excluding the value at the K index, to create a dictionary list.

It has a time complexity of O(n) and Auxiliary space of O(n).

Example

# initialize a list
demo_list = [2, 3, 4, 9, 8]

# printing the list
print("List : " + str(demo_list))

# initializing K
K = 3

# Convert list to Single Dictionary Key Value Pair
res = {demo_list[K]: []}
for i in range(len(demo_list)):
   if i != K:
      res[demo_list[K]].append(demo_list[i])

# printing result
print("Result : " + str(res))

Output

List : [2, 3, 4, 9, 8]
Result : {9: [2, 3, 4, 8]}

Method 2: Using List Slicing

Another approach could be a one liner, if we slice out the K-th index and assign as values in the dictionary.

The code below will make things clearer.

It has a time complexity of O(n) and Auxiliary space of O(n).

Example

# initializing list
demo_list = [2, 3, 4, 9, 8]

# printing the list
print("List : " + str(demo_list))

# initializing K
K = 2

# Convert list to Single Dictionary Key Value Pair
res = {demo_list[K]: demo_list[:K] + demo_list[K + 1:]}

# result
print("Result : " + str(res))

Output

List : [2, 3, 4, 9, 8]
Result : {4: [2, 3, 9, 8]}

Method 3: Using List/Dictionary Comprehension

In this approach, a new list is created that contains all the elements, except the one at index K from the demo/initial list. Then, it will create a new dictionary with the key at index K and the new list as value.

It has a time complexity of O(n) and Auxiliary space of O(n).

Example

# initialize a list
demo_list = [2, 3, 4, 9, 8]

# initializing K
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(demo_list) if i != K]
 
# creating a dictionary
res = {demo_list[K]: new_list}

# printing initial list
print("List : " + str(demo_list))
# printing result
print("Result : " + str(res))

Output

List : [2, 3, 4, 9, 8]
Result : {9: [2, 3, 4, 8]}

Similarly we can do the same using Dictionary Comprehension. The time complexity and auxiliary space remains the same.

Method 4: Using Set Operations

One more way could be using the set operations. Where we initialize the initial list and index K. Create a set with the index key and subtract key from values to get a set of all the values that are not at index K.

Create a dictionary using these sets. Print the original list and final dictionary.

It has a time complexity of O(n) and Auxiliary space of O(n).

Example

# initializing list
demo_list = [2, 3, 4, 9, 8]

# initializing K
K = 3

# Using set operations
keys = {demo_list[K]}
values = set(demo_list) - keys
res = {keys.pop(): list(values)}

# printing the list
print("List : " + str(demo_list))
# printing Dictionary
print("Result : " + str(res))

Output

List : [2, 3, 4, 9, 8]
Result : {9: [8, 2, 3, 4]}

*Now, try implementing the same using pop() method

Method 5: Filter and Lambda Function

In this method, we will create a lambda function if each element in the list is not equal to the element at index given K. And use the filter() function to extract the list that passes the lambda condition. Then we can use that resulting list to further generate a dictionary.

Example

# initializing a list
demo_list = [2, 3, 4, 9, 8]
# initializing K
K = 1
res = {demo_list[K]: list(filter(lambda x: x != demo_list[K], demo_list))}
# printing list
print("List : " + str(demo_list))
# printing result
print("Result : " + str(res))

Output

List : [2, 3, 4, 9, 8]
Result : {3: [2, 4, 9, 8]}

It has a time complexity of O(n) and Auxiliary space of O(n).

Conclusion

Hence, we had a glance at various ways to convert a list into a single dictionary key value pair. In python we have several ways to implement a function or logic. We need to keep in mind the best way feasible as per requirement.

Sometimes, the time it takes to execute is what matters and sometimes the delay, sometimes the format matters as well. As we see the outputs vary slightly often.

Updated on: 18-Aug-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements