- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Assign ids to each unique value in a Python list
While using Python dictionary we may need to identify each element of the dictionary uniquely. For that we have to assign unique IDs to each of the element in the dictionary. In this article we will see how to assign the same unique Id to an element if it is repeated in a Python dictionary.
With enumerate() and OrderedDict.fromkeys()
The enumerate function expands a given dictionary by adding a counter to each element of the dictionary. Then we apply the OrderedDict.fromkeys() which will extract the same value of the counter from the dictionary hence eliminating the duplicates values of IDs.
Example
from collections import OrderedDict Alist = ['Mon','Tue','Wed','Mon',5,3,3] print("The given list : ",Alist) # Assigning ids to values list_ids = [{v: k for k, v in enumerate( OrderedDict.fromkeys(Alist))} [n] for n in Alist] # The result print("The list of ids : ",list_ids)
Output
Running the above code gives us the following result −
The given list : ['Mon', 'Tue', 'Wed', 'Mon', 5, 3, 3] The list of ids : [0, 1, 2, 0, 3, 4, 4]
Using OrderedDict from collections
In this method we use the defaultdict function which will assign a new key only to the new elements. Then we use the lambda function to loop through the new list of unique Ids.
Example
from collections import defaultdict # Given List Alist = ['Mon','Tue','Wed','Mon',5,3,3] print("The given list : ",Alist) # Assigning ids to values d_dict = defaultdict(lambda: len(d_dict)) list_ids= [d_dict[n] for n in Alist] # Print ids of the dictionary print("The list of ids : ", list_ids)
Output
Running the above code gives us the following result −
The given list : ['Mon', 'Tue', 'Wed', 'Mon', 5, 3, 3] The list of ids : [0, 1, 2, 0, 3, 4, 4]
- Related Articles
- Assign value to unique number in list in Python
- Python Program to assign each list element value equal to its magnitude order
- Program to find number of unique people from list of contact mail ids in Python
- Python – Assign Alphabet to each element
- Python - Unique keys count for Value in Tuple List
- Python program to unique keys count for Value in Tuple List
- Assign range of elements to List in Python
- Assign multiple variables with a Python list values
- How do I assign a dictionary value to a variable in Python?
- How do we assign a value to several variables simultaneously in Python?
- Python - Assign a specific value to Non Max-Min elements in Tuple
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Get unique values from a list in Python
- How to Assign a Serial Number to Duplicate or Unique Values in Excel?
- Python Program to print unique values from a list
