Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Array Methods (Complete Guide for Beginners)

 In Python, arrays are used to store multiple values of the same data type. To work efficiently with arrays, Python provides several built-in array methods that allow you to add, remove, modify, search, and manipulate data easily.

In this tutorial, you will learn all important Python array methods with clear examples and explanations.


Creating an Array

We use Python’s array module.

import array as arr

numbers = arr.array('i', [10, 20, 30, 40, 50])
print(numbers)

1. append() Method

Adds an element at the end of the array.

Example

numbers.append(60)
print(numbers)

Output

array('i', [10, 20, 30, 40, 50, 60])

2. insert() Method

Inserts an element at a specific position.

Example

numbers.insert(2, 99)
print(numbers)

Output

array('i', [10, 20, 99, 30, 40, 50])

3. remove() Method

Removes the first occurrence of a value.

Example

numbers.remove(99)
print(numbers)

Output

array('i', [10, 20, 30, 40, 50])

4. pop() Method

Removes an element by index.

Example

numbers.pop(1)
print(numbers)

Output

array('i', [10, 30, 40, 50])

5. index() Method

Returns the index of the first occurrence of a value.

Example

print(numbers.index(30))

Output

2

6. reverse() Method

Reverses the array in place.

Example

numbers.reverse()
print(numbers)

Output

array('i', [50, 40, 30, 20, 10])

7. extend() Method

Adds multiple elements to the array.

Example

numbers.extend([60, 70, 80])
print(numbers)

Output

array('i', [10, 20, 30, 40, 50, 60, 70, 80])

8. count() Method

Returns how many times a value appears.

Example

print(numbers.count(20))

Output

1

9. tolist() Method

Converts array into a Python list.

Example

list_version = numbers.tolist()
print(list_version)

Output

[10, 20, 30, 40, 50]

10. fromlist() Method

Adds elements from a list into an array.

Example

numbers.fromlist([90, 100])
print(numbers)

Output

array('i', [10, 20, 30, 40, 50, 90, 100])

Real-World Example: Student Marks System

import array as arr

marks = arr.array('i', [85, 90, 78, 88])

marks.append(92)
marks.remove(78)
marks.reverse()

print(marks)

Real-World Example: Inventory Management

import array as arr

inventory = arr.array('i', [10, 20, 30, 40])

inventory.extend([50, 60])
inventory.pop(2)

print(inventory)

Array Methods Summary Table

MethodDescription
append()Add element at end
insert()Add element at position
remove()Remove value
pop()Remove by index
index()Find position
reverse()Reverse array
extend()Add multiple items
count()Count occurrences
tolist()Convert to list
fromlist()Add from list

Common Mistakes

Mistake 1: Using wrong data type

❌ Wrong:

arr.array('i', [10, "hello"])

✔ Arrays must contain same type


Mistake 2: Using remove() for index

numbers.remove(2)

✔ This removes value, not index


Mistake 3: Forgetting return value of pop()

numbers.pop()

✔ Returns removed value


Safe Usage Example

if 20 in numbers:
    numbers.remove(20)

Conclusion

Python array methods make it easy to manage and manipulate data efficiently.

You learned:

  • How to add, remove, and update arrays
  • How to search and transform data
  • Built-in array methods
  • Real-world usage examples

Mastering array methods is essential for data handling, analytics, and real-world programming tasks.




Post a Comment

0 Comments