![]() |
VOOZH | about |
Given two lists, one of key and other values, convert it to dictionary with list values, if keys map to different values on basis of index, add in its value list.
Input : test_list1 = [5, 6, 6, 6], test_list2 = [8, 3, 2, 9]
Output : {5: [8], 6: [3, 2, 9]}
Explanation : Elements with index 6 in corresponding list, are mapped to 6.Input : test_list1 = [6, 6, 6, 6], test_list2 = [8, 3, 2, 9]
Output : {6: [8, 3, 2, 9]}
Explanation : All mapped to single number.
This is one of the ways in which this task can be performed. In this, we perform mapping the keys to required values using zip() and loop is used to perform iteration of keys.
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {5: [8, 10], 6: [3, 2, 4], 4: [9]}Time Complexity: O(n*n) where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list
The combination of above functions can be used to solve this problem. In this, we perform task as one liner and defaultdict() is used to preassign values with empty lists.
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {5: [8, 10], 6: [3, 2, 4], 4: [9]}Algorithm:
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {5: [8, 10], 6: [3, 2, 4], 4: [9]}The time complexity of this algorithm is O(n), where n is the length of the keys and values lists. This is because the function processes each key-value pair exactly once and performs a constant amount of work for each pair.
The auxiliary space complexity of the algorithm is O(k), where k is the number of unique keys in the keys list. This is because the function creates a list for each unique key in the keys list, and the size of each list is proportional to the number of occurrences of the key in the keys list. The result dictionary also requires O(k) space to store the keys and lists. In the worst case where all the keys in keys are unique, the space complexity is O(n).
Here's a step-by-step approach to the code, along with the time and space complexity:
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {5: [8, 10], 6: [3, 2, 4], 4: [9]}Time complexity: The time complexity of this code is O(n), where n is the length of test_list1 or test_list2. This is because the code iterates through each element of the lists exactly once.
Auxiliary Space: The space complexity of this code is also O(n), where n is the length of test_list1 or test_list2. This is because the defaultdict object res will have at most n keys, and each key will have a list of values whose length is at most n.
Use a dictionary comprehension along with the built-in zip function to achieve the same result as the previous methods.
Step-by-step approach:
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {5: [8, 10], 6: [3, 2, 4], 4: [9]}Time Complexity: O(n), where n is the length of the input lists, due to the iteration over the list of tuples.
Auxiliary Space: O(n), where n is the length of the input lists, due to the space required to store the dictionary.
Step-by-Step Approach:
Below is the implementation of the above approach:
The mapped dictionary: {4: [9], 5: [8, 10], 6: [2, 3, 4]}Time complexity: O(n log n)
Auxiliary space: O(n) for the dictionary created with dictionary comprehension
Method #7: Using numpy:
Algorithm:
Output:
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary: {4: [9], 5: [8, 10], 6: [3, 2, 4]}
Time Complexity:
Initializing the lists and combining them into a 2D array takes O(n) time, where n is the length of the lists.
Getting the unique values using numpy's unique() method takes O(n log n) time in the worst case.
Creating the dictionary comprehension takes O(n^2) time in the worst case because for each unique value, it needs to search for the corresponding values in the 2D array.
Printing the dictionary takes O(n) time because it needs to iterate through all n keys and values.
Therefore, the overall time complexity of the algorithm is O(n^2) in the worst case.
Space Complexity:
The space required to store the two lists is O(n).
The space required to store the 2D array is also O(n).
The space required to store the unique values of the first column is O(k), where k is the number of unique values.
The space required to store the dictionary is O(kn), where k is the number of unique values and n is the average length of the corresponding lists.
There are no other significant variables used in the algorithm.
Therefore, the overall space complexity of the algorithm is O(kn).
Approach
The original list 1 is : [5, 6, 6, 4, 5, 6]
The original list 2 is : [8, 3, 2, 9, 10, 4]
The mapped dictionary : {4: [9], 5: [8, 10], 6: [3, 2, 4]}
Time Complexity : O(M * N) M -length of x N - length of test_list2
Auxiliary Space : O(N) N - length of res dictionary