![]() |
VOOZH | about |
Sometimes, while working with data, we can have a problem in which we need to get alike index elements in a single container. This means the columns of Matrix/Multiple lists need to be converted to list of tuples, each comprising of like index elements. Let's discuss certain ways in which this task can be done.
Method #1 : Using zip() + list comprehension The combination of above functions can work together to achieve this particular task. In this, we combine the like index elements into column using zip(), and binding all tuples into a list and iteration of dictionary lists is performed by list comprehension.
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. AND operation - Using zip() + list comprehension performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using zip() + values() The combination of above functions can be used to perform this task. In this, we access the values of dictionary values using the values() and aggregating columns can be done using zip().
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]Method #3: Using numpy.column_stack()
Note: Install numpy module using command "pip install numpy"
Using numpy.column_stack() converts lists to column tuples by using the numpy.column_stack() function. The method first imports the numpy library, then initializes a dictionary "test_dict" containing three lists. Then it prints the original dictionary. The numpy.column_stack() function is then used to stack the lists in the dictionary side by side. It takes a list of lists and forms a 2D array by stacking the input lists as columns.
Output:
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [[ 9 6 1]
[ 1 7 4]
[11 4 5]]
Time complexity: O(n) where n is the number of elements in the lists as numpy.column_stack() is a constant time operation, regardless of the number of input lists.
Auxiliary space: O(n) as it creates a new 2D array of tuples.
Method #4: Using pandas.DataFrame
Pandas is a popular library for data manipulation and analysis in Python. We can use the DataFrame function of pandas to create a table from the dictionary and then select the columns to create tuples.
Here is the step-by-step approach:
Output:
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.
Method #5 : Using nested for loop + values() method
Step-by-step approach:
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]Time Complexity: O(M*N) M - length of values list N - length of each list in value list
Auxiliary Space: O(M*N) M - length of values list N - length of each list in value list
Method #6 : Using heapq :
Algorithm :
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (1, 4, 7), (4, 5, 11)]Time complexity:
The for loop iterates over len(test_dict) elements, which is the length of any list in test_dict.
The list comprehension extracts the values at the current index from each list in test_dict, taking O(len(test_dict)) time.
The heapq.nsmallest() function takes O(len(test_dict) log len(test_dict)) time to return the smallest n elements.
Therefore, the overall time complexity of the algorithm is O(len(test_dict)^2 log len(test_dict)).
Space complexity:
The space required to store the res list is O(len(test_dict)^2).
The space required to store the temp list is O(len(test_dict)).
Therefore, the overall space complexity of the algorithm is O(len(test_dict)^2).
Method #7: Using itertools:
Algorithm :
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]The time complexity : O(n*m) where n is the number of keys in test_dict and m is the length of the longest list among the values. This is because the zip_longest() method and map() function both iterate through each element of the lists, and the tuple() function has a constant time complexity.
The space complexity : O(m) because we are creating a new tuple for each column of values, and the size of each tuple is proportional to the length of the longest list among the values. Additionally, we are creating a new list to store the resulting tuples of columns.
Method #8: Using reduce():
Algorithm:
The original dictionary is : {'list1': [1, 4, 5], 'list2': [6, 7, 4], 'list3': [9, 1, 11]}
Like index column tuples are : [(1, 6, 9), (4, 7, 1), (5, 4, 11)]
Time complexity:
The zip_longest function iterates over the values in test_dict and creates tuples with fillvalue when necessary. The time complexity of zip_longest is O(n*m), where n is the number of keys in test_dict and m is the length of the longest list.
The reduce function iterates over the tuples created by zip_longest and applies the lambda function to them. The time complexity of reduce is O(n), where n is the number of tuples.
Therefore, the overall time complexity of the code is O(n*m).
Space complexity:
The zip_longest function creates tuples with fillvalue when necessary. The number of tuples created is the length of the longest list in test_dict. Therefore, the space complexity of zip_longest is O(m).
The reduce function creates a new list with the column tuples. The length of this list is equal to the number of tuples created by zip_longest. Therefore, the space complexity of reduce is also O(m).
Therefore, the overall space complexity of the code is O(m).