Arrays in Python: Representation & Operations
1. Understanding Arrays in Python
In Python, arrays are most commonly implemented using lists.
A list is a collection of ordered elements that can store integers, strings, floats, or even other lists.
Unlike arrays in lower-level languages like C, Python lists are dynamic (they can grow or shrink during
runtime) and can hold different data types.
Python also has a more specific array type in the array module, which stores elements of a specific data
type, but lists are more commonly used for teaching array concepts because of their simplicity and
flexibility.
2. Representation of Arrays in Python
Arrays (lists) are stored in contiguous memory blocks, and each element is accessed by an index,
starting at 0.
Example:
Key points:
Indexing starts at 0.
Access time is constant: O(1).
Lists allow duplicate values.
3. Traversing Arrays
Traversal means visiting each element of the array to process it.
Python techniques:
Using a for loop with index:
Using a for loop directly:
Why traversal matters: It’s the first step before insertion, deletion, or searching.
4. Insertion in Arrays
Insertion adds a new element at a specific position in the list.
append() → adds element at the end of the list.
insert(index, value) → adds element at a specific index.
Behind the scenes:
When inserting in the middle, elements are shifted to make space → time complexity is O(n).
5. Deletion in Arrays
Deletion removes an element from the list.
Python methods:
del list[index] → removes by index.
remove(value) → removes first occurrence of value.
pop(index) → removes and returns element at index (default last element).
Example:
Behind the scenes: Deletion causes shifting → time complexity is O(n) for most cases.
6. Combining Traversal, Insertion, and Deletion
A practical approach is combining these operations in a menu-driven program so students understand
how they work together.
Example idea:
Menu options: Insert, Delete, Traverse, Exit
Example Menu-Driven Program
# Initial array
numbers = [10, 20, 30]
while True:
print("\n=== Array Operations Menu ===")
print("1. Traverse array")
print("2. Insert element")
print("3. Delete element")
print("4. Exit")
choice = input("Enter choice (1-4): ")
if choice == "1":
print("Array elements:")
for value in numbers:
print(value, end=" ")
print()
elif choice == "2":
index = int(input("Enter index to insert: "))
value = int(input("Enter value to insert: "))
if index >= 0 and index <= len(numbers):
[Link](index, value)
print(f"After insertion: {numbers}")
else:
print("Invalid index!")
elif choice == "3":
index = int(input("Enter index to delete: "))
if index >= 0 and index < len(numbers):
del numbers[index]
print(f"After deletion: {numbers}")
else:
print("Invalid index!")
elif choice == "4":
print("Exiting program.")
break
else:
print("Invalid choice! Please enter a number between 1 and 4.")
7. Real-World Applications of Arrays
Arrays (lists) in Python are used in:
Storing sequences of data (e.g., student scores, inventory lists)
Image representation (2D arrays for pixels)
Simple databases
Queues and stacks
Example real-world problem: A program that stores a shopping list, allows insertion of new items,
deletion of purchased items, and displaying the current list.