🐍2.3Mastering Python Lists: Essential Methods and Deep vs. Shallow Copy
Mastering Python Lists: Essential Methods and Deep vs. Shallow Copy
Lists in Python are one of the most versatile data structures, offering a variety of built-in methods to manipulate data effectively. In this blog, we'll explore some essential list methods using an example list and then delve into the difference between shallow and deep copies.
Essential List Methods with Examples
Let's start with a sample list:
# Define the list
my_list = [1, 2, 3, 3, 4, 5, ("A", "B", "B", "C", "D"), [11, 12, 13, 1, 2, 3], "DIVYA", "SANKET", "VENU"]
1. append()
- Adding an element at the end
my_list.append(100)
print("After append:", my_list)
✅ Adds 100
at the end of the list.
2. extend()
- Extending with another list
my_list.extend([200, 300])
print("After extend:", my_list)
✅ Adds elements [200, 300]
to the list.
3. insert()
- Inserting an element at a specific index
my_list.insert(2, "NEW_ELEMENT")
print("After insert:", my_list)
✅ Inserts "NEW_ELEMENT"
at index 2
.
4. remove()
- Removing the first occurrence of an element
my_list.remove(3)
print("After remove:", my_list)
✅ Removes the first occurrence of 3
.
5. pop()
- Removing and returning the last element
popped_element = my_list.pop()
print("Popped Element:", popped_element)
print("After pop:", my_list)
✅ Removes and returns "VENU"
(last element).
6. clear()
- Clearing all elements
# my_list.clear()
# print("After clear:", my_list)
✅ Removes all elements from the list.
7. index()
- Finding the index of an element
index_sanket = my_list.index("SANKET")
print("Index of SANKET:", index_sanket)
✅ Returns the index of "SANKET"
.
8. count()
- Counting occurrences of an element
count_3 = my_list.count(3)
print("Count of 3:", count_3)
✅ Counts occurrences of 3
in the list.
9. sort()
- Sorting a list
numeric_list = [5, 1, 3, 2, 4]
numeric_list.sort()
print("After sort:", numeric_list)
✅ Works only for lists with the same data type.
10. reverse()
- Reversing the list
my_list.reverse()
print("After reverse:", my_list)
✅ Reverses the order of elements in the list.
11. copy()
- Creating a shallow copy of the list
copied_list = my_list.copy()
print("Copied List:", copied_list)
✅ Creates a duplicate of my_list
without modifying the original.
Deep Copy vs. Shallow Copy in Python
When copying lists, there are two primary types:
- Shallow Copy (
copy()
) → Creates a new list but keeps references to nested elements. - Deep Copy (
copy.deepcopy()
) → Creates a completely independent copy, including nested lists.
Shallow Copy (copy()
) Example
import copy
# Original list with nested elements
original_list = [1, 2, [3, 4], ("A", "B")]
# Shallow copy
shallow_copy_list = original_list.copy()
# Modifying the nested list in the original list
original_list[2][0] = 100
print("Original List:", original_list) # Output: [1, 2, [100, 4], ("A", "B")]
print("Shallow Copy:", shallow_copy_list) # Output: [1, 2, [100, 4], ("A", "B")]
✅ Changes in the nested list reflect in the copy!
Deep Copy (copy.deepcopy()
) Example
import copy
# Original list with nested elements
original_list = [1, 2, [3, 4], ("A", "B")]
# Deep copy
deep_copy_list = copy.deepcopy(original_list)
# Modifying the nested list in the original list
original_list[2][0] = 100
print("Original List:", original_list) # Output: [1, 2, [100, 4], ("A", "B")]
print("Deep Copy:", deep_copy_list) # Output: [1, 2, [3, 4], ("A", "B")]
✅ Deep copy ensures nested lists are independent!
Key Differences
Feature | Shallow Copy (copy() ) |
Deep Copy (copy.deepcopy() ) |
---|---|---|
Copies list? | ✅ Yes | ✅ Yes |
Copies nested elements? | ❌ No (only references) | ✅ Yes (creates new objects) |
Modifications affect original? | ✅ Yes | ❌ No |
Performance | 🚀 Faster, uses less memory | 🐢 Slower, uses more memory |
Conclusion
Mastering list methods is essential for Python programming, whether you're dealing with data manipulation or complex structures. Understanding shallow and deep copies helps prevent unintended modifications and optimizes performance. Choose the right method based on your use case!
🔥 Happy Coding! 🚀
Comments
Post a Comment