New Array with Only Even Elements
Create new array containing only even elements.
IntermediatePhase 4 - Level 3: TransformationExample 2 of 10
new-array-with-only-even-elements.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Filter even elements8even_arr = []9for element in arr:10 if element % 2 == 0:11 even_arr.append(element)1213print(f"Original: {arr}")14print(f"Even elements: {even_arr}")
Output
Enter array size: 5 Element 1: 2 Element 2: 3 Element 3: 4 Element 4: 5 Element 5: 6 Original: [2, 3, 4, 5, 6] Even elements: [2, 4, 6]
What's going on
Filter elements based on condition.
Key Concepts:
Check if element is even
Append to new array if condition met
Filter operation
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum