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 | Sort list according to other list order
- Python - Rearrange list by other list order
- Python | Slicing list from Kth element to last element
- Python | Insert Nth element to Kth element in other list
- Python | Replace elements in second list with index of same element in first list
- Python | Move one list element to another list
- Python | Indices list of matching element from other list
- Python | Subtract two list elements if element in first list is greater
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Count occurrences of an element in a list
- Python | Count the elements in a list until an element is a Tuple
- Python | Count the sublists containing given element in a list
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python | Inserting item in sorted list maintaining order
- Python | Finding relative order of elements in list
- Python | Extract unique tuples from list, Order Irrespective
- Python | Reverse Order Sort in String List
- Python - Order Tuples by List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.