Python Copy Lists | All Ways to Copy a List with Examples

Python Copy Lists

Copying a list means creating a new list with the same elements. It's important to understand shallow vs deep copy to avoid unintended side-effects.

List को copy करने का मतलब है उसी elements वाली एक नई list बनाना। Shallow और Deep copy को समझना जरूरी है ताकि अनचाहे बदलाव से बचा जा सके।

Example 1: Copy Using Slicing
original = [1, 2, 3, 4]
copied = original[:]
print('Original:', original)
print('Copied:', copied)

Copy list using slicing (creates a shallow copy).

Slicing से list copy करें (shallow copy बनती है)।

Output:

Original: [1, 2, 3, 4]
Copied: [1, 2, 3, 4]
Example 2: Copy Using list() Constructor
original = ['a', 'b', 'c']
copied = list(original)
print('Original:', original)
print('Copied:', copied)

Copy list by converting it to a new list with list().

list() से नई list बनाकर copy करें।

Output:

Original: ['a', 'b', 'c']
Copied: ['a', 'b', 'c']
Example 3: Copy Using copy() Method
original = [10, 20, 30]
copied = original.copy()
print('Original:', original)
print('Copied:', copied)

Copy list using the built-in copy() method (shallow copy).

copy() method से list copy करें (shallow copy)।

Output:

Original: [10, 20, 30]
Copied: [10, 20, 30]
Example 4: Shallow Copy vs Original Modification
import copy
original = [[1,2], [3,4]]
shallow_copy = original.copy()
shallow_copy[0][0] = 9
print('Original:', original)
print('Shallow Copy:', shallow_copy)

Shows that shallow copy copies references; modifying inner objects affects both lists.

Shallow copy references copy करता है; अंदर के objects बदलने से दोनों list प्रभावित होती हैं।

Output:

Original: [[9, 2], [3, 4]]
Shallow Copy: [[9, 2], [3, 4]]
Example 5: Deep Copy to Avoid Reference Issues
import copy
original = [[1,2], [3,4]]
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 9
print('Original:', original)
print('Deep Copy:', deep_copy)

Deep copy creates a completely independent copy; changes in copy don’t affect original.

Deep copy पूरी तरह स्वतंत्र copy बनाता है; copy में बदलाव से original प्रभावित नहीं होता।

Output:

Original: [[1, 2], [3, 4]]
Deep Copy: [[9, 2], [3, 4]]
Example 6: Copy List of Immutable Elements
original = (1, 2, 3)
copied = list(original)
print('Copied List:', copied)

Copy tuple to list using list() constructor.

tuple को list में list() से copy करें।

Output:

Copied List: [1, 2, 3]
Example 7: Copy List Using List Comprehension
original = [1, 2, 3]
copied = [x for x in original]
print('Copied:', copied)

Copy list using list comprehension (shallow copy).

List comprehension से list copy करें (shallow copy)।

Output:

Copied: [1, 2, 3]
Example 8: Using copy Module’s copy() Function
import copy
original = [1, 2, 3]
copied = copy.copy(original)
print('Copied:', copied)

copy.copy() performs a shallow copy similar to list.copy().

copy.copy() भी shallow copy करता है।

Output:

Copied: [1, 2, 3]
Example 9: Copy List of Dictionaries
import copy
original = [{'a':1}, {'b':2}]
shallow = original.copy()
deep = copy.deepcopy(original)
shallow[0]['a'] = 9
print('Original:', original)
print('Shallow:', shallow)
print('Deep:', deep)

Demonstrates shallow vs deep copy on list of dicts.

List of dictionaries पर shallow और deep copy का फर्क दिखाया।

Output:

Original: [{'a': 9}, {'b': 2}]
Shallow: [{'a': 9}, {'b': 2}]
Deep: [{'a': 1}, {'b': 2}]
Example 10: Copy Empty List and Append
original = [1, 2, 3]
copied = original[:]
copied.append(4)
print('Original:', original)
print('Copied:', copied)

Copy list and modify copied list without affecting original.

Copy list बनाकर उसमें बदलाव करें, original list unaffected।

Output:

Original: [1, 2, 3]
Copied: [1, 2, 3, 4]