2025 Python Arrays
Introduction
Arrays in Python are used to store multiple values of the same data type in a structured way. They are more memory-efficient than lists for handling large numerical data.

The array
module in Python provides various methods to manipulate arrays, such as adding, removing, searching, and modifying elements. Understanding these methods helps in efficient data handling and processing.
Python Arrays
Introduction
Arrays in Python are used to store multiple values of the same data type in a single variable. They provide a structured way to handle large amounts of data efficiently. Unlike lists, arrays in Python require elements to be of a fixed type. While Python does not have a built-in array data type, the array
module allows us to create and manipulate arrays.
This guide will cover everything about Python arrays, including their creation, modification, looping, and comparison with lists, along with FAQs for a deeper understanding.
1. Creating an Array in Python
To use arrays in Python, we must first import the array
module:
import array
# Creating an array of integers
numbers = array.array('i', [10, 20, 30, 40, 50])
print(numbers) # Output: array('i', [10, 20, 30, 40, 50])
Explanation:
'i'
→ Type code representing integer values.[10, 20, 30, 40, 50]
→ The actual elements of the array.
Common Type Codes Used in Arrays
Type Code | Data Type |
---|---|
'i' | Integer |
'f' | Float |
'd' | Double (Large float) |
'b' | Signed Byte |
'u' | Unicode Character |
2. Accessing Elements in an Array
You can access array elements using indexing, just like in lists.
print(numbers[0]) # Output: 10
print(numbers[2]) # Output: 30
Negative indexing also works:
print(numbers[-1]) # Output: 50 (Last element)
3. Modifying an Array
Python allows modification of elements in an array by referencing their index.
a) Changing an Element
numbers[1] = 25
print(numbers) # Output: array('i', [10, 25, 30, 40, 50])
b) Adding Elements
append(value)
→ Adds an element at the end.insert(index, value)
→ Inserts an element at a specific position.
numbers.append(60) # Adds 60 at the end
numbers.insert(2, 35) # Inserts 35 at index 2
print(numbers) # Output: array('i', [10, 25, 35, 30, 40, 50, 60])
4. Removing Elements from an Array
To delete elements, use:
remove(value)
→ Removes the first occurrence of the given value.pop(index)
→ Removes an element at a specific index (default is last element).
numbers.remove(25) # Removes 25 from the array
numbers.pop(3) # Removes the element at index 3
print(numbers) # Output: array('i', [10, 35, 30, 50, 60])
5. Looping Through an Array
Using a for
loop
for num in numbers:
print(num)
Output:
10
35
30
50
60
Using a while
loop
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
6. Finding the Length of an Array
Use len()
to get the number of elements in an array.
print(len(numbers)) # Output: 5
7. Difference Between Lists and Arrays in Python
Feature | Lists | Arrays |
---|---|---|
Data Type | Can store mixed data types | Requires elements of the same type |
Performance | Slower for numerical operations | Faster for numerical operations |
Import Required? | No, lists are built-in | Yes, import array is needed |
Best for | General-purpose use | Numerical and memory-efficient operations |
Accessing Elements of an Array in Python
Introduction
An array stores multiple values in a single variable. Each value in an array has a unique index that helps in accessing it. In Python, you can retrieve, modify, and work with array elements using their index values.

Python does not have a built-in array type, but the array
module allows us to create arrays that store elements of the same type. Let’s explore how to access array elements in simple steps.
1. Creating an Array
Before accessing elements, let’s first create an array using the array
module:
import array
# Creating an array of integers
numbers = array.array('i', [10, 20, 30, 40, 50])
print(numbers) # Output: array('i', [10, 20, 30, 40, 50])
2. Accessing Elements by Index
Each element in an array is stored at a unique index starting from 0
.
Index | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
Value | 10 | 20 | 30 | 40 | 50 |
To access an element, specify its index inside square brackets []
:
print(numbers[0]) # Output: 10
print(numbers[2]) # Output: 30
print(numbers[4]) # Output: 50
Important Points:
- The first element is at index 0.
- The last element is at index length – 1.
- Trying to access an index that doesn’t exist will cause an IndexError.
3. Using Negative Indexing
Python allows negative indexing, where -1
refers to the last element, -2
to the second last, and so on.
print(numbers[-1]) # Output: 50 (Last element)
print(numbers[-3]) # Output: 30 (Third element from the end)
Negative Index | -5 | -4 | -3 | -2 | -1 |
---|---|---|---|---|---|
Value | 10 | 20 | 30 | 40 | 50 |
Negative indexing helps when working with the last elements of an array.
4. Accessing Elements Using a Loop
You can use a loop to access multiple elements easily.
a) Using a for
loop
for num in numbers:
print(num)
Output:
10
20
30
40
50
b) Using a while
loop
i = 0
while i < len(numbers):
print(numbers[i])
i += 1
5. Finding the Length of an Array
Use the len()
function to find the total number of elements in an array.
print(len(numbers)) # Output: 5
Python Array Methods
Introduction
Arrays in Python are used to store multiple values of the same data type. The array module provides various built-in methods to manipulate arrays, such as adding, removing, searching, and modifying elements. These methods make working with arrays easier and more efficient.

Creating an Array in Python
Before using array methods, the array module must be imported to create an array.
import array
numbers = array.array('i', [10, 20, 30, 40, 50])
print(numbers)
Append Method
The append method adds a new element to the end of the array.
numbers.append(60)
print(numbers)
Insert Method
The insert method allows adding an element at a specific position.
numbers.insert(2, 25)
print(numbers)
Remove Method
This method removes the first occurrence of a specified value in the array.
numbers.remove(30)
print(numbers)
Pop Method
The pop method removes and returns an element from a specified index. If no index is given, it removes the last element.
removed_value = numbers.pop(3)
print(numbers)
print(removed_value)
Index Method
This method returns the position of the first occurrence of a given value in the array.
position = numbers.index(50)
print(position)
Reverse Method
The reverse method changes the order of elements in an array.
numbers.reverse()
print(numbers)
Count Method
The count method returns the number of times a value appears in an array.
count_10 = numbers.count(10)
print(count_10)
Extend Method
The extend method adds multiple values from another array or list to the existing array.
extra_numbers = array.array('i', [70, 80, 90])
numbers.extend(extra_numbers)
print(numbers)
Buffer Info Method
This method provides the memory address and the number of elements in an array.
print(numbers.buffer_info())
To List Method
This method converts an array into a Python list.
num_list = numbers.tolist()
print(num_list)
How can elements be added to an array in Python
The append method adds an element to the end, while the insert method adds it at a specific position.
numbers.append(100)
numbers.insert(2, 55)
What is the difference between remove and pop methods
The remove method deletes a specific value, while the pop method removes an element at a given index and returns it.
numbers.remove(30)
popped_element = numbers.pop(2)
How to reverse an array in Python
The reverse method is used to reverse the elements in an array.
numbers.reverse()
print(numbers)
How to find the index of an element in an array
The index method helps in finding the position of the first occurrence of a value in the array.
pos = numbers.index(50)
print(pos)
2025 Python Arrays – FAQs
1. What is the difference between a Python list and an array?
A list can store different data types, while an array stores only one type of data, making it more memory-efficient for numerical operations.
2. How do I add or remove elements from a Python array?
Use append(value)
to add an element, insert(index, value)
to add at a specific position, remove(value)
to delete a specific value, and pop(index)
to remove an element at a given index.
3. How do I find the index of an element in an array?
Use the index(value)
method to get the position of the first occurrence of the element.
4. How do I convert an array into a list in Python?
Use the tolist()
method to convert an array into a list. This allows easier manipulation using list operations.
Summary
Python arrays offer a way to store and manipulate multiple elements of the same type efficiently. The array
module provides built-in methods like append()
, insert()
, remove()
, pop()
, reverse()
, and count()
, among others, to work with arrays. These methods allow for easy modification and retrieval of data. Unlike lists, arrays in Python require all elements to be of the same type, making them ideal for numerical operations.