Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Convert List to Index and Value dictionary

Python – Convert List to Index and Value dictionary

Given a List, convert it to dictionary, with separate keys for index and values.

Input : test_list = [3, 5, 7, 8, 2, 4, 9], idx, val = “1”, “2” 
Output : {‘1’: [0, 1, 2, 3, 4, 5, 6], ‘2’: [3, 5, 7, 8, 2, 4, 9]} 
Explanation : Index and values mapped at similar index in diff. keys., as “1” and “2”. 

Input : test_list = [3, 5, 7], idx, val = “1”, “2” 
Output : {‘1’: [0, 1, 2], ‘2’: [3, 5, 7]} 
Explanation : Index and values mapped at similar index in diff. keys., as “1” and “2”.

Method : Using loop + enumerate()

In this, we iterate for list elements using enumerate() to get index along with values, and append the values and indices accordingly in separate dictionaries accordingly.

Python3




# Python3 code to demonstrate working of
# Convert List to Index and Value dictionary
# Using loop + enumerate()
 
# initializing list
test_list = [3, 5, 7, 8, 2, 4, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing keys for index and vals
idx, val = "indx", "vals"
 
# initializing empty mesh
res = {idx : [], val : []}
for id, vl in enumerate(test_list):
    res[idx].append(id)
    res[val].append(vl)
         
# printing results
print("Constructed dictionary : " + str(res))


Output

The original list is : [3, 5, 7, 8, 2, 4, 9]
Constructed dictionary : {'indx': [0, 1, 2, 3, 4, 5, 6], 'vals': [3, 5, 7, 8, 2, 4, 9]}

Time Complexity: O(n), where n is the length of the dictionary
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary

Method 2: Using zip() and dictionary comprehension:

First uses zip() to create a list of tuples where each tuple contains an index-value pair. Then, it uses dictionary comprehension to convert the list of tuples to a dictionary with keys “indx” and “vals“, where each key’s value is a list of the corresponding indices or values. Finally, it prints the resulting dictionary.

Python3




test_list = [3, 5, 7, 8, 2, 4, 9]
 
# using zip() to create a list of tuples, where each tuple contains an index-value pair
pairs = list(zip(range(len(test_list)), test_list))
 
# using dictionary comprehension to convert the list of tuples to a dictionary with keys "indx" and "vals"
res = { "indx": [i for i, v in pairs], "vals": [v for i, v in pairs] }
 
# print the resulting dictionary
print(res)


Output

{'indx': [0, 1, 2, 3, 4, 5, 6], 'vals': [3, 5, 7, 8, 2, 4, 9]}

Time Complexity: O(N), where N is the length of the input list.
Auxiliary Space: O(N)

Method 3: Use the built-in dict() constructor with a generator expression

Step-by-step approach:

  • Creates a dictionary named result with two keys:
    ‘indx’: a list of the indices of the values in test_list, created using the range() function and the len() function to generate a list of integers from 0 to 6 (the length of test_list minus 1).
    ‘vals’: a reference to the original test_list.
  • Finally, the code prints the original list and the constructed dictionary

Python3




test_list = [3, 5, 7, 8, 2, 4, 9]
result = {'indx': list(range(len(test_list))), 'vals': test_list}
 
print("The original list is:", test_list)
print("Constructed dictionary:", result)


Output

The original list is: [3, 5, 7, 8, 2, 4, 9]
Constructed dictionary: {'indx': [0, 1, 2, 3, 4, 5, 6], 'vals': [3, 5, 7, 8, 2, 4, 9]}

Time complexity: O(n), where n is the length of the list, because it only requires iterating over the list once to create the key-value pairs. 
Auxiliary space: O(n), because the resulting dictionary will contain n key-value pairs.

RELATED ARTICLES

Most Popular

Recent Comments