In Python, arrays are used to store multiple values of the same data type. Once an array is created, you may need to add new items while working with dynamic data.
Python provides several methods to add elements to an array such as append(), insert(), and extend().
In this tutorial, you will learn different ways to add items to arrays with clear examples.
Creating an Array
We use the array module in Python.
import array as arr
numbers = arr.array('i', [10, 20, 30])
print(numbers)1. Add Item Using append()
The append() method adds an item at the end of the array.
Example
numbers.append(40)
print(numbers)Output
array('i', [10, 20, 30, 40])Explanation
- Adds one item at the end
- Most commonly used method
2. Add Item Using insert()
The insert() method adds an item at a specific position.
Syntax
array.insert(index, value)Example
numbers.insert(1, 15)
print(numbers)Output
array('i', [10, 15, 20, 30, 40])Explanation
- Inserts
15at index1 - Existing elements shift to the right
3. Add Multiple Items Using extend()
The extend() method adds multiple items to the array.
Example
numbers.extend([50, 60, 70])
print(numbers)Output
array('i', [10, 15, 20, 30, 40, 50, 60, 70])Explanation
- Adds multiple values at once
- Useful for merging data
4. Adding Items Using Loop
You can also add items one by one using a loop.
Example
for i in [80, 90]:
numbers.append(i)
print(numbers)Output
array('i', [10, 15, 20, 30, 40, 50, 60, 70, 80, 90])5. Add Item at Beginning
Using insert(0, value) you can add an item at the start.
Example
numbers.insert(0, 5)
print(numbers)Output
array('i', [5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90])6. Add Item Based on Condition
You can add items dynamically based on logic.
Example
for i in range(100, 121, 10):
if i not in numbers:
numbers.append(i)
print(numbers)Real-World Example: Student Marks
import array as arr
marks = arr.array('i', [85, 90, 78])
marks.append(88)
marks.insert(1, 95)
print(marks)Output
array('i', [85, 95, 90, 78, 88])Real-World Example: Product Prices
import array as arr
prices = arr.array('f', [99.99, 149.50])
prices.append(200.75)
prices.extend([250.00, 300.50])
print(prices)Difference Between Methods
| Method | Description |
|---|---|
| append() | Add one item at end |
| insert() | Add item at specific index |
| extend() | Add multiple items |
Common Mistakes
Mistake 1: Using append() for multiple items
❌ Wrong:
numbers.append([50, 60])✔ This adds a list inside array (not separate items)
Mistake 2: Wrong Data Type
arr.array('i', [10, "hello"])✔ Arrays only accept same data type
Mistake 3: Wrong Index in insert()
numbers.insert(100, 50)✔ It will still work but may place item at end
Array Add Methods Summary
| Method | Use |
| append() | Add single item at end |
| insert() | Add item at position |
| extend() | Add multiple items |
| loop + append | Dynamic insertion |
Practice Exercise 1
Add 100 at the end of the array.
Practice Exercise 2
Insert 25 at index 2.
Conclusion
Adding items to arrays in Python is simple and flexible. You can:
- Add single items using
append() - Insert at specific positions using
insert() - Add multiple items using
extend() - Use loops for dynamic data insertion
These methods are essential for real-world programming, especially in data processing and numerical applications.


0 Comments