π Python Arrays β Access, Add, Remove, and Loop Through Items
π§² Introduction β Why Learn Python Array Operations?
Arrays in Python (using the built-in array
module) are efficient for storing homogeneous data types like integers or floats. Once created, you often need to access values, add or remove items, and iterate over the array.
Mastering these operations helps in performance-critical applications like data logging, scientific computing, and real-time systems.
π― In this guide, youβll learn:
- How to access elements using indexing
- How to append, insert, or extend an array
- How to remove elements using
pop()
orremove()
- How to loop through arrays efficiently
π§ Creating a Python Array
from array import array
nums = array('i', [10, 20, 30, 40])
β Explanation:
'i'
is the type code for signed integers.nums
is now an array of integers.
π 1. Accessing Array Elements
print(nums[0]) # Output: 10
print(nums[-1]) # Output: 40
β Explanation:
- Access via zero-based indexing like lists.
- Negative indexes count from the end.
β 2. Adding Items to an Array
β
Using .append()
nums.append(50)
print(nums) # array('i', [10, 20, 30, 40, 50])
β Adds an element to the end of the array.
β
Using .insert()
nums.insert(1, 15)
print(nums) # array('i', [10, 15, 20, 30, 40, 50])
β
Inserts value 15
at index 1
.
β
Using .extend()
nums.extend([60, 70])
print(nums) # array('i', [10, 15, 20, 30, 40, 50, 60, 70])
β Appends multiple elements from a list or iterable.
ποΈ 3. Removing Items from an Array
β
Using .remove()
nums.remove(30)
print(nums) # array('i', [10, 15, 20, 40, 50, 60, 70])
β Removes the first occurrence of the specified value.
β
Using .pop()
nums.pop(2)
print(nums) # Removes item at index 2 (value 20)
β Removes the item at the given index and returns it.
π 4. Looping Through an Array
for num in nums:
print(num)
β Explanation:
- Python arrays are iterable. You can use
for
loops just like with lists.
π Bonus: Reverse and Slice
print(nums[::-1]) # Reversed: [70, 60, 50, 40, 15, 10]
print(nums[1:4]) # Sliced: [15, 40, 50]
β Use slicing techniques for subarrays and reversing.
π‘ Best Practices
- β
Use
append()
for adding one element,extend()
for multiple. - β
Use
remove()
by value andpop()
by index. - β Use slicing for partial data extraction or reversal.
- β Avoid using
array
for mixed typesβuse lists instead.
π Summary β Recap & Next Steps
Python arrays provide a clean and efficient way to handle fixed-type numeric data. You can easily access, insert, remove, and iterate over array items with built-in methods.
π Key Takeaways:
- β Arrays use indexing like lists for access.
- β
Use
append()
,insert()
,extend()
to add items. - β
Use
remove()
andpop()
for deletion. - β Arrays are iterable and support slicing.
βοΈ Real-World Relevance:
Used in numeric computation, memory-sensitive applications, and real-time data systems, Python arrays help optimize performance and maintain simplicity.
β FAQ Section β Python Array Access, Add, Remove, Loop
β How do I access a value in an array?
β Use index notation:
arr[0]
β How do I add multiple items at once?
β
Use .extend()
:
arr.extend([5, 6, 7])
β What is the difference between pop()
and remove()
?
β
pop()
removes by index, remove()
removes by value:
arr.pop(2)
arr.remove(30)
β Can I use loops with arrays?
β Yes. Arrays are iterable:
for val in arr:
print(val)
β Can arrays be reversed or sliced?
β Yes. Use slicing:
arr[::-1]
arr[1:4]
Share Now :