Given a list, extract range of consecutive similar elements.
Input : test_list = [2, 3, 3, 3, 8, 8]
Output : [(2, 0, 0), (3, 1, 3), (8, 4, 5)]
Explanation : 2 occurs from 0th to 0th index, 3 from 1st to 3rd index.
Input : test_list = [3, 3, 3]
Output : [(3, 0, 3)]
Explanation : 3 from 0th to 3rd index.
Approach: Using loop
This is a brute way to tackle this problem. In this, we loop for each element and get a similar element range. These are traced and appended in list accordingly with elements.
The original list is : [2, 3, 3, 3, 8, 8, 6, 7, 7]
Elements with range : [(2, 0, 0), (3, 1, 3), (8, 4, 5), (6, 6, 6), (7, 7, 8)]
- Python - Extract elements from Ranges in List
- Python - Extract elements with Range consecutive occurrences
- Python - Extract Similar pairs from List
- Python | Extract similar index elements
- Python - Extract Missing Ranges
- Python - Extract Similar Key Values
- Python program to extract characters in given range from a string list
- Python - Similar Consecutive elements frequency
- Python | Sorting list of lists with similar list elements
- Python | Convert String ranges to list
- Python - Extract tuples with elements in Range
- Python - Extract range characters from String
- Python - Extract Dictionary values list to List
- Python - Extract dictionary items with List elements
- Python - Extract Kth index elements from Dictionary Value list
- Python - Extract records if Kth elements not in List
- Python program to extract the Unique Dictionary Value List elements
- Python | Extract length of longest string in list
- Python | Extract Score list of String
- Python - Extract words starting with K in String 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 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.