Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 4 - Level 4: Aggregation & ComparisonExample 1 of 10
compare-arrays-same-order.py
1# Take first array
2n1 = int(input("Enter size of first array: "))
3arr1 = []
4for i in range(n1):
5 arr1.append(int(input(f"Array1 element {i+1}: ")))
6
7# Take second array
8n2 = int(input("Enter size of second array: "))
9arr2 = []
10for i in range(n2):
11 arr2.append(int(input(f"Array2 element {i+1}: ")))
12
13# Compare
14if 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. 1

    Read both lists with their own sizes.

  2. 2

    Evaluate arr1 == arr2.

  3. 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.