Compare Arrays (Same Order)
Compare two Python lists with order-sensitive equality. Students read separate sizes and see why matching values alone are not enough when positions differ.
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# Compare14if arr1 == arr2:15 print("Arrays are equal")16else:17 print("Arrays are not equal")
Output
Enter size of first array: 3 Array1 element 1: 1 Array1 element 2: 2 Array1 element 3: 3 Enter size of second array: 3 Array2 element 1: 1 Array2 element 2: 2 Array2 element 3: 3 Arrays are equal
In this one
- Compare corresponding positions
- Include length in equality
- Preserve duplicate counts
How it works
- 1
Read both lists with their own sizes.
- 2
Evaluate arr1 == arr2.
- 3
Print whether ordered sequence equality holds.
Edge cases
Are two empty lists equal?
Are two empty lists equal?
Yes.
Does [1, 2] equal [2, 1]?
Does [1, 2] equal [2, 1]?
No. Their order differs.
What's going on
arr1 == arr2 compares lengths and corresponding values. It does not merely ask whether the same distinct integers occur.
Lists [1, 2] and [2, 1] differ because their positions differ. Repeated elements also matter. The comparison leaves both lists untouched and produces one message after input collection; two empty lists compare equal.
Are two empty lists equal? Yes.
Does [1, 2] equal [2, 1]? No. Their order differs.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What must match?
Pick an answer to check it.
Question 2
Are two empty lists equal?
Pick an answer to check it.
Questions people ask
Are two empty lists equal?
Yes.
Does [1, 2] equal [2, 1]?
No. Their order differs.
What must match?
Length and corresponding values.
๐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