Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python – Check if elements index are equal for list elements
When it is required to check if the index of elements matches their values or compare elements at the same positions across lists, we can use simple iteration with the enumerate() function.
Basic Example: Check if Index Equals Value
Here's how to check if any element's index equals its value ?
data = [0, 2, 1, 3, 5]
print("The list is:")
print(data)
# Check if any element equals its index
index_equals_value = []
for index, element in enumerate(data):
if index == element:
index_equals_value.append((index, element))
print("Elements where index equals value:")
print(index_equals_value)
The list is: [0, 2, 1, 3, 5] Elements where index equals value: [(0, 0), (3, 3)]
Comparing Two Lists at Same Indices
Let's check if elements at corresponding positions are equal between two lists ?
list1 = [12, 62, 19, 79, 58, 0, 99]
list2 = [12, 74, 19, 54, 58, 0, 11]
print("The first list is:")
print(list1)
print("The second list is:")
print(list2)
# Compare elements at same indices
equal_positions = []
for index in range(min(len(list1), len(list2))):
if list1[index] == list2[index]:
equal_positions.append(index)
print("Positions where elements are equal:")
print(equal_positions)
print("Values at equal positions:")
for pos in equal_positions:
print(f"Index {pos}: {list1[pos]}")
The first list is: [12, 62, 19, 79, 58, 0, 99] The second list is: [12, 74, 19, 54, 58, 0, 11] Positions where elements are equal: [0, 2, 4, 5] Values at equal positions: Index 0: 12 Index 2: 19 Index 4: 58 Index 5: 0
Advanced Example with Condition Checking
Here's a more complex scenario where we check equality with additional conditions ?
list1 = [12, 62, 19, 79, 58, 0, 99]
list2 = [12, 74, 19, 54, 58, 0, 11]
check_values = [19, 58, 62]
print("List 1:", list1)
print("List 2:", list2)
print("Check values:", check_values)
# Sort both lists for comparison
list1_sorted = sorted(list1)
list2_sorted = sorted(list2)
print("Sorted list 1:", list1_sorted)
print("Sorted list 2:", list2_sorted)
# Check if elements differ at same index AND element is in check_values
result = True
for index, element in enumerate(list1_sorted):
if index < len(list2_sorted):
if list1_sorted[index] != list2_sorted[index] and element in check_values:
result = False
print(f"Difference found at index {index}: {element} vs {list2_sorted[index]}")
break
print("Final result:", "Equal" if result else "Not equal")
List 1: [12, 62, 19, 79, 58, 0, 99] List 2: [12, 74, 19, 54, 58, 0, 11] Check values: [19, 58, 62] Sorted list 1: [0, 12, 19, 58, 62, 79, 99] Sorted list 2: [0, 11, 12, 19, 54, 58, 74] Difference found at index 4: 62 vs 54 Final result: Not equal
Using List Comprehension
A more concise approach using list comprehension ?
numbers = [0, 1, 5, 3, 4]
# Find all positions where index equals value
matches = [(i, val) for i, val in enumerate(numbers) if i == val]
print("Original list:", numbers)
print("Index-value matches:", matches)
# Check if all elements equal their indices
all_match = all(i == val for i, val in enumerate(numbers))
print("All elements match their index:", all_match)
Original list: [0, 1, 5, 3, 4] Index-value matches: [(0, 0), (1, 1), (4, 4)] All elements match their index: False
Conclusion
Use enumerate() to access both index and value when checking index-element relationships. List comprehensions provide a concise way to find matches, while traditional loops offer more control for complex conditions.
Advertisements
