Skip to content

ENUMERATE WITH FILTERING AND TRANSFORMATIONS

Python
#!/usr/bin/env python3
"""
ENUMERATE WITH FILTERING AND TRANSFORMATIONS
Combine enumerate with conditionals for data processing
"""

print("=== Extract Even Positions with Values ===")
numbers = [10, 20, 30, 40, 50, 60]

even_positions = [(i, num) for i, num in enumerate(numbers) if i % 2 == 0]
print(f"Even positions: {even_positions}")

print("\n=== Mark Items Above Threshold ===")
scores = [45, 78, 92, 65, 88, 34, 91]
threshold = 70

for i, score in enumerate(scores, start=1):
    status = "PASS" if score >= threshold else "FAIL"
    print(f"Student {i}: {score} - {status}")

print("\n=== Find First Occurrence ===")
items = ["apple", "banana", "apple", "cherry", "apple"]
search = "apple"

for i, item in enumerate(items):
    if item == search:
        print(f"First '{search}' found at index {i}")
        break

print("\n=== Replace Elements at Specific Positions ===")
data = [1, 2, 3, 4, 5, 6, 7, 8]
print(f"Original: {data}")

# Replace every third element with 0
result = [0 if i % 3 == 0 else val for i, val in enumerate(data)]
print(f"Modified: {result}")

print("\n=== Count by Position Type ===")
values = [5, 10, 15, 20, 25, 30]
even_pos_sum = sum(val for i, val in enumerate(values) if i % 2 == 0)
odd_pos_sum = sum(val for i, val in enumerate(values) if i % 2 == 1)

print(f"Sum at even positions: {even_pos_sum}")
print(f"Sum at odd positions: {odd_pos_sum}")