Merge Two Arrays
Merge two arrays into one.
IntermediatePhase 4 - Level 4: Aggregation & ComparisonExample 3 of 10
merge-two-arrays.py
1# Take first array2n1 = int(input("Enter size of first array: "))3arr1 = []4for i in range(n1):5 arr1.append(int(input(f"Array1 element {i+1}: ")))67# Take second array8n2 = int(input("Enter size of second array: "))9arr2 = []10for i in range(n2):11 arr2.append(int(input(f"Array2 element {i+1}: ")))1213# Merge14merged = arr1 + arr215print(f"Merged array: {merged}")
Output
Enter size of first array: 3 Array1 element 1: 1 Array1 element 2: 2 Array1 element 3: 3 Enter size of second array: 2 Array2 element 1: 4 Array2 element 2: 5 Merged array: [1, 2, 3, 4, 5]
What's going on
Use + operator to concatenate arrays.
Key Concepts:
+ operator concatenates lists
Creates new array
Preserves order
🔗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