# Python – Elements with same index

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

When it is required to display elements with same index, a simple iteration and the ‘enumerate’ attribute is used.

Below is a demonstration of the same −

## Example

Live Demo

my_list = [33, 1, 2, 45, 41, 13, 6, 9]

print("The list is :")
print(my_list)

my_result = []
for index, element in enumerate(my_list):
if index == element:
my_result.append(element)

print("The result is :")
print(my_result)

## Output

The list is :
[33, 1, 2, 45, 41, 13, 6, 9]
The result is :
[1, 2, 6]

## Explanation

• A list is defined and displayed on the console.

• An empty list is defined.

• The list is iterated over, and the element is compared with the index.

• If it is equal, it is appended to the empty list.

• This is the output that is displayed on the console.

Updated on 06-Sep-2021 08:04:04