
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python – Index Value repetition in List
When it is required to find the index value that has been repeated in a list, it is iterated over using the list comprehension and ‘enumerate’.
Example
Below is a demonstration of the same
my_list = [4, 0, 3, 1] print("The list is :") print(my_list) my_result = [element for sub in ([index] * element for index, element in enumerate(my_list)) for element in sub] print("The result is :") print(my_result)
Output
The list is : [4, 0, 3, 1] The result is : [0, 0, 0, 0, 2, 2, 2, 3]
Explanation
A list is defined and is displayed on the console.
List comprehension is used to iterate through the index values of the list.
The ‘enumerate’ is used to give values to elements of the list.
This is assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Element repetition in list in Python
- Accessing index and value in a Python list
- Python – Replace value by Kth index value in Dictionary List
- How does repetition operator work on list in Python?
- Python – Convert List to Index and Value dictionary
- Python – Sort Dictionary List by Key’s ith Index value
- Program to find largest kth index value of one list in Python
- Index minimum value Record in Python
- Python Index specific cyclic iteration in list
- Equate two list index elements in Python
- Python – Negative index of Element in List
- How to remove index list from another list in python?
- Python - Character repetition string combinations
- Python – Append List every Nth index
- Dictionary with index as value in Python

Advertisements