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

Merge Two Arrays

Merge two lists by concatenation. Students follow input collection and understand why combining sequences does not sort values or remove repeated entries.

IntermediatePhase 4 - Level 4: Aggregation & ComparisonExample 3 of 10
merge-two-arrays.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# Merge
14merged = arr1 + arr2
15print(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]

In this one

  • Concatenate sequences
  • Preserve each input block
  • Distinguish merge from sorting

How it works

  1. 1

    Collect arr1 and arr2.

  2. 2

    Create merged with arr1 + arr2.

  3. 3

    Print the new list without modifying either source.

Edge cases

Does this sort the result?

Does this sort the result?

No. It concatenates in input order.

What if both lists are empty?

What if both lists are empty?

The merged list is empty.

What's going on

arr1 + arr2 creates a new list with all first-list entries followed by all second-list entries.

This merge is concatenation, not the ordered merge step from merge sort. Original list order is preserved within each block and duplicates remain. An empty input list contributes no entries, while the other list still appears in full.

Does this sort the result? No. It concatenates in input order.

What if both lists are empty? The merged list is empty.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which entries come first?

Pick an answer to check it.

Question 2

Does this sort the result?

Pick an answer to check it.

Questions people ask

Does this sort the result?

No. It concatenates in input order.

What if both lists are empty?

The merged list is empty.

Which entries come first?

All entries of arr1.