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)]
- Python | Count occurrences of an element in a list
- Python | Count the sublists containing given element in a list
- Python | Count the elements in a list until an element is a Tuple
- Python | Sort list according to other list order
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python | Indices list of matching element from other list
- Python | Insert Nth element to Kth element in other list
- Python | Slicing list from Kth element to last element
- Python | Finding relative order of elements in list
- Python | Reverse Order Sort in String List
- Python | Move one list element to another list
- Python | Extract unique tuples from list, Order Irrespective
- Python | Inserting item in sorted list maintaining order
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.