Python has several built-in data structures, each with its own set of features and use cases. Here, we’ll cover the most common ones: list, tuple, and array. I’ll explain their key characteristics, common operations, methods, and potential pitfalls.
1. Lists
Definition:
A list is a mutable, ordered collection of items. It can hold elements of different types (integers, strings, objects, etc.).
Common Operations and Methods:
# Creating a list
my_list = [1, 2, 3, "four", 5.0]
# Accessing elements (zero-based indexing)
print(my_list[0]) # Output: 1
# Modifying an element
my_list[1] = "two"
print(my_list) # Output: [1, "two", 3, "four", 5.0]
# Appending an element
my_list.append(6)
print(my_list) # Output: [1, "two", 3, "four", 5.0, 6]
# Inserting an element at a specific index
my_list.insert(2, "inserted")
print(my_list) # Output: [1, "two", "inserted", 3, "four", 5.0, 6]
# Removing an element by value
my_list.remove("four")
print(my_list) # Output: [1, "two", "inserted", 3, 5.0, 6]
# Popping an element (removes the last element by default)
removed = my_list.pop() # Output: 6
print(my_list) # Output: [1, "two", "inserted", 3, 5.0]
# Slicing a list
sub_list = my_list[1:3]
print(sub_list) # Output: ["two", "inserted"]
# List concatenation
new_list = my_list + [7, 8, 9]
print(new_list) # Output: [1, "two", "inserted", 3, 5.0, 7, 8, 9]
Common Mistakes:
- Modifying lists during iteration: Modifying the list while iterating over it can lead to unexpected behavior.
my_list = [1, 2, 3, 4]
for i in my_list:
if i == 2:
my_list.remove(i) # Modifying list during iteration
# Use a copy or filter instead to avoid issues.
- Using append() incorrectly: Many confuse append() (which adds a single element) with extend() (which concatenates another iterable).
my_list = [1, 2]
my_list.append([3, 4]) # Adds a list as a single element
print(my_list) # Output: [1, 2, [3, 4]]
my_list.extend([3, 4]) # Adds elements individually
print(my_list) # Output: [1, 2, 3, 4]
- List mutability: Since lists are mutable, if you assign one list to another variable (e.g., list2 \= list1), both variables point to the same list. Use list.copy() or slicing ([:]) to avoid this. python list1 = [1, 2, 3] list2 \= list1 Both refer to the same object list2.append(4) print(list1) # Output: [1, 2, 3, 4]
2. Tuples
Definition:
A tuple is an immutable, ordered collection of items. Once created, its elements cannot be changed.
Common Operations and Methods:
# Creating a tuple
my_tuple = (1, 2, 3, "four", 5.0)
# Accessing elements
print(my_tuple[0]) # Output: 1
# Tuple unpacking
a, b, c, d, e = my_tuple
print(b) # Output: 2
# Concatenation
new_tuple = my_tuple + (6, 7)
print(new_tuple) # Output: (1, 2, 3, "four", 5.0, 6, 7)
# Slicing a tuple
sub_tuple = my_tuple[1:4]
print(sub_tuple) # Output: (2, 3, "four")
# Finding an index of an element
index = my_tuple.index("four")
print(index) # Output: 3
# Counting occurrences
count = my_tuple.count(3)
print(count) # Output: 1
Common Mistakes:
- Confusion with single-element tuples: A single-element tuple must have a trailing comma, otherwise it’s treated as a normal value.
single_tuple = (1) # Not a tuple, just an integer
actual_tuple = (1,) # A tuple with one element
- Immutability: Tuples cannot be changed, which makes them great for read-only collections. However, if a tuple contains mutable objects (like a list), the contents of those mutable objects can still be modified.
my_tuple = (1, [2, 3])
my_tuple[1].append(4)
print(my_tuple) # Output: (1, [2, 3, 4])
3. Arrays
Definition:
The array module provides arrays in Python. Arrays are more memory-efficient than lists when dealing with large amounts of numerical data. However, arrays can only store elements of the same type (e.g., all integers or all floats).
Common Operations and Methods:
# Creating an array of integers
int_array = array.array('i', [1, 2, 3, 4, 5])
# Accessing elements
print(int_array[0]) # Output: 1
# Appending to the array
int_array.append(6)
print(int_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
# Inserting at a specific index
int_array.insert(1, 7)
print(int_array) # Output: array('i', [1, 7, 2, 3, 4, 5, 6])
# Removing an element by value
int_array.remove(7)
print(int_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
# Slicing an array
sub_array = int_array[2:5]
print(sub_array) # Output: array('i', [3, 4, 5])
Common Mistakes:
- Using an incompatible type code: When creating an array, you must provide a type code to indicate the type of elements (e.g., ‘i’ for integers, ‘f’ for floats). If you try to append an incompatible type, you’ll get an error.
float_array = array.array('i', [1.0, 2.0]) # TypeError: integer argument expected, got float
- Confusing arrays with lists: Arrays are more constrained than lists in Python, as they only allow elements of a specific type. Use lists when flexibility is needed.
- Arrays are faster and more memory-efficient for numerical data, but they are less flexible than lists. You cannot store elements of different types (like strings and integers together).