Python | Assign ids to each unique value in a list
Sometimes, while working in web development domain or in competitive programming, we require to assign a unique id to each of the different values to track its occurrence for the count or any other required use case. Let’s discuss certain ways in which this task can be performed.
Method #1: Using defaultdict + lambda + list comprehension
The combination of the above functions can be used to accomplish this particular task. The defaultdict function performs the main task of assigning Ids using the lambda function, it assigns the current number of keys to every new key. The list comprehension is used in later stage to form the list.
Python3
from collections import defaultdict
test_list = [ 5 , 6 , 7 , 6 , 5 , 1 ]
print ( "The original list : " + str (test_list))
temp = defaultdict( lambda : len (temp))
res = [temp[ele] for ele in test_list]
print ( "The ids of assigned values is : " + str (res))
|
Output
The original list : [5, 6, 7, 6, 5, 1]
The ids of assigned values is : [0, 1, 2, 1, 0, 3]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using defaultdict + lambda + list comprehension which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using OrderedDict.fromkeys() + enumerate() + list comprehension
This method performs tasks similar to the above method. In this orderedDict.fromkeys function performs the function to remove the duplicates and enumerate function helps us get the indices of values to map them together.
Python3
from collections import OrderedDict
test_list = [ 5 , 6 , 7 , 6 , 5 , 1 ]
print ( "The original list : " + str (test_list))
res = [{val: key for key, val in enumerate (
OrderedDict.fromkeys(test_list))}
[ele] for ele in test_list]
print ( "The ids of assigned values is : " + str (res))
|
Output
The original list : [5, 6, 7, 6, 5, 1]
The ids of assigned values is : [0, 1, 2, 1, 0, 3]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using in , not in operators and index() method
Python3
test_list = [ 5 , 6 , 7 , 6 , 5 , 1 ]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
if i not in x:
x.append(i)
res = []
for i in test_list:
res.append(x.index(i))
print ( "The ids of assigned values is : " + str (res))
|
Output
The original list : [5, 6, 7, 6, 5, 1]
The ids of assigned values is : [0, 1, 2, 1, 0, 3]
Method #4: Using reduce():
Algorithm :
- Initialize an empty list unique_values to store the unique values encountered in the test_list.
- Initialize an empty list res to store the ids of the values in the test_list.
- Define a function assign_id that takes two arguments acc and val, where acc is the accumulated value and val is the current value being processed
- Check if the val is already present in the unique_values list.
- If the val is not present, append it to the unique_values list.
- Append the index of the val in the unique_values list to the res list.
- Return the unique_values list from the assign_id function
- Use the reduce() function to iterate over the test_list and apply the assign_id function to each element of the list.
- The reduce() function returns the final value of unique_values, which is not used in this implementation.
- Print the res list.
Python3
from functools import reduce
test_list = [ 5 , 6 , 7 , 6 , 5 , 1 ]
print ( "The original list : " + str (test_list))
unique_values = []
res = []
def assign_id(acc, val):
if val not in unique_values:
unique_values.append(val)
res.append(unique_values.index(val))
return unique_values
reduce (assign_id, test_list, [])
print ( "The ids of assigned values is : " + str (res))
|
Output
The original list : [5, 6, 7, 6, 5, 1]
The ids of assigned values is : [0, 1, 2, 1, 0, 3]
The time complexity: O(n^2) because the unique_values.index() method called in the loop has a time complexity of O(n).
The space complexity : O(n) because we are storing all the unique values and their indices in separate lists.
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...