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

Copy Array Manually

Copy integer list elements manually with append. Students create a list container and distinguish a copied list from a second reference to the same list.

IntermediatePhase 4 - Level 3: TransformationExample 10 of 10
copy-array-manually.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Copy manually
8new_arr = []
9for element in arr:
10 new_arr.append(element)
11
12print(f"Original: {arr}")
13print(f"Copy: {new_arr}")

Output

Enter array size: 4
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Original: [1, 2, 3, 4]
Copy: [1, 2, 3, 4]

In this one

  • Create a new container
  • Copy by traversal
  • Distinguish shallow copying from aliasing

How it works

  1. 1

    Initialize new_arr empty.

  2. 2

    Append each original element in order.

  3. 3

    Display both lists without rebinding them together.

Edge cases

What if arr is empty?

What if arr is empty?

new_arr remains an independent empty list.

Is this a deep copy of nested objects?

Is this a deep copy of nested objects?

No. Appending an object keeps a reference to that object.

What's going on

new_arr begins as its own empty list. The loop appends each original integer in order.

Both lists print the same values, but they are distinct containers. Assigning new_arr = arr instead would create another reference to the same container. With nested mutable objects, this element-wise approach would still share those objects; this exercise stores only integers.

What if arr is empty? new_arr remains an independent empty list.

Is this a deep copy of nested objects? No. Appending an object keeps a reference to that object.

Quiz

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

Question 1

What does new_arr = arr do instead?

Pick an answer to check it.

Question 2

What if arr is empty?

Pick an answer to check it.

Questions people ask

What if arr is empty?

new_arr remains an independent empty list.

Is this a deep copy of nested objects?

No. Appending an object keeps a reference to that object.

What does new_arr = arr do instead?

Aliases the original list.