πŸ“ Python Arrays
Estimated reading: 3 minutes 21 views

πŸ“ 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() or remove()
  • 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 and pop() 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() and pop() 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Arrays: Access / Add / Remove / Loop Items

Or Copy Link

CONTENTS
Scroll to Top