Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element’s occurrence in that list. This type of problem can come across many domains. Let’s discuss a way in which this problem can be solved.
Method : Using
defaultdict() + loop
We can perform this task using defaultdict() and loop by carefully assigning and incrementing order of elements.
The original list : [1, 4, 1, 5, 4, 1, 5] List elements with their order count : [(1, 1), (4, 1), (1, 2), (5, 1), (4, 2), (1, 3), (5, 2)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.