Open In App

Assign Ids to Each Unique Value in a List – Python

Last Updated : 30 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of assigning unique IDs to each value in a list involves iterating over the elements and assigning a unique identifier to each distinct value. Since lists in Python are mutable, the goal is to efficiently assign an ID to each unique element while ensuring that repeated elements receive the same ID. For example, consider the list a = [5, 6, 7, 6, 5, 1]. The objective is to assign a unique ID to each element such that the resulting list of IDs represents the unique elements in a. In this case, the output might look like [0, 1, 2, 1, 0, 3], where each unique number in a (5, 6, 7, 1) is assigned a unique ID (0, 1, 2, 3) and repeated values reuse their assigned ID.

Using dictionary comprehension

Dictionary comprehension offers a efficient way to assign IDs to each unique value in a list. The idea is to use the setdefault() ensure that each unique value is assigned a unique ID based on the length of the dictionary which grows as new elements are encountered . This method efficiently handles duplicates by skipping already encountered values, keeping the solution clean and minimal.

Python
a = [5, 6, 7, 6, 5, 1]

b = {} # initialize empty list
res = [b.setdefault(ele, len(b)) for ele in a]

print(res)

Output
[0, 1, 2, 1, 0, 3]

Explanation: This code uses setdefault() assign a unique ID to each element in list a. If an element is already in the dictionary b, it reuses the assigned ID otherwise, it assigns a new ID based on the current length of b. The result is a list of IDs corresponding to each element in a.

Using enumerate()

enumerate() provides both the index and the value while iterating over the list. By using this function with a dictionary, we can track unique values and assign them IDs based on their first occurrence. This method ensures that IDs are assigned only once to each unique element and utilizes the dictionary to store and look up the IDs efficiently.

Python
a = [5, 6, 7, 6, 5, 1]

b = {} # initialize empty list
res = [b.setdefault(ele, len(b)) if ele not in b else b[ele] for i, ele in enumerate(a)]

print(res)

Output
[0, 1, 2, 1, 0, 3]

Explanation: list comprehension iterates over each element in a using enumerate(). It checks if the element is already in the dictionary b if not, it assigns a new ID based on the current length of b using setdefault(). If the element is already present, it reuses the existing ID from b.

Using for loop

Using a traditional for loop is a more explicit approach for assigning unique IDs. In this method, we manually check if each element has already been encountered by checking the dictionary. If not, we assign a new ID based on the number of unique elements encountered so far.

Python
a = [5, 6, 7, 6, 5, 1]

b = {} # initialize empty dictionary
res = [] # initialize empty list
for ele in a :
    if ele not in b:
        b[ele] = len(b)
    res.append(b[ele])

print(res)

Output
[0, 1, 2, 1, 0, 3]

Explanation: This code iterates over a, assigns a unique ID to each element in b if not already assigned, and appends the element’s ID to the list res. Repeated elements reuse their previously assigned ID.



Next Article
Practice Tags :

Similar Reads