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.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Copy manually8new_arr = []9for element in arr:10 new_arr.append(element)1112print(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
Initialize new_arr empty.
- 2
Append each original element in order.
- 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.
๐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