![]() |
VOOZH | about |
Sometimes, while working with data, we can have problems in which we need to categorize a particular list and values to similar keys. This can be a problem in counting data. Like counting votes or counting coins. Let's discuss certain ways in which this task can be performed.
Method #1: Using loop This is the brute way in which this task can be performed. In this, we run a loop to add values to the dictionary value list, if not present we dynamically create a key and perform append.
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time Complexity: O(n), where n is the number of elements in the list. The time complexity is linear because the loop iterates through each element in the list and checks if it exists in the dictionary. If it does, it appends the element, and if it doesn't, it creates a new key-value pair.
Auxiliary Space: O(n), where n is the number of elements in the list. The auxiliary space is linear because the dictionary stores each unique element in the list as a key, with a value of a list containing the duplicates.
Method #2: Using defaultdict() + loop The combination of the above functions can be used to solve this problem. In this, pre-initializeize the dictionary using defaultdict().
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time complexity: O(n), where n is the number of elements in the test_list.
Auxiliary space: O(n), where n is the number of elements in the test_list.
Method #3 : Using list(),set(),count() methods
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Nikhil': ['Nikhil'], 'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Akash': ['Akash', 'Akash'], 'Akshat': ['Akshat', 'Akshat']}
Time Complexity: O(n^2), where n is the length of the input list
Auxiliary Space: O(n), as we are using a dictionary to store the result, which can have up to n key-value pairs in the worst case.
Method #4 : Using get():
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using defaultdict() + list comprehension
Use a defaultdict(list) to create a dictionary where each key is initialized to an empty list. Then iterate over each item in the test_list and append it to the corresponding key in the dictionary. Finally, convert the defaultdict back to a regular dictionary using a dictionary comprehension.
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), where n is the length of the test_list. This is because we need to create a dictionary that has a key for each unique value in the test_list, which requires additional memory.
Method #6: Using setdefault() method
Follow the below steps to implement the above idea:
Below is the implementation of the above approach:
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using groupby from itertools
In this method, we groups the elements in the list based on their values. In this case, we first sort the list using sorted(). Then, we iterate over the groups of elements with the same value and add them to the dictionary with the key being the value of the group.
Output:
The original list is : ['Manjeet', 'Nikhil', 'Akshat', 'Akash', 'Manjeet', 'Akash', 'Akshat', 'Manjeet']
The similar values dictionary is : {'Manjeet': ['Manjeet', 'Manjeet', 'Manjeet'], 'Nikhil': ['Nikhil'], 'Akshat': ['Akshat', 'Akshat'], 'Akash': ['Akash', 'Akash']}
Time Complexity: O(nlogn), due to the initial sorting operation
Auxiliary Space: O(n), for the dictionary created in the end.