Array in Python | Set 1 (Introduction and Functions)
Last Updated :
26 Jul, 2025
In Python, Arrays are a type of container that can store elements of the same data type more efficiently. They are provided by the built-in array module and are useful when working with large amounts of numeric data where performance and memory efficiency matter.
Why do we need Arrays?
- Efficiently store and manage large collections of data of same type.
- Consume less memory and offer faster performance than lists for numerical operations.
- Ideal for mathematical and scientific computations where data type consistency is crucial.
- Support element-wise operations and indexing for quick data manipulation.
- Useful for working with low-level data structures and when interfacing with C or binary files.
Properties of Arrays
- Each array element is of the same data type and size. For example: For an array of integers with the int data type, each element of the array will occupy 4 bytes.
- Elements of array are stored in contiguous memory locations.
Array Function
The array() function from Python's array module creates an array with elements of a specified data type. It is used to store homogeneous data.
Syntax:
array(typecode, [value1, value2, ...])
Parameter:
- typecode: A single character representing data type (e.g., 'i' for integers, 'f' for floats).
- value list: A list (or iterable) of values matching specified typecode.
Some data types are mentioned in table below:
Type Code | C Type | Python Type | Minimum size in Bytes |
---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | Py_UNICODE | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'q' | signed long long | int | 8 |
'Q' | unsigned long long | int | 8 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Operations on Array
Python arrays support various built-in methods to manipulate and manage their elements efficiently. These operations help in adding, removing, searching or modifying data within the array.
Let’s explore each array method one by one with a simple explanation and example:
Append() Method
append() method adds a specified value to the end of the array.
Example:
This code demonstrates how to create an integer array using array module and then append a new value to it using append() method. It first prints original array, adds number 4 at the end and finally displays updated array.
Python
import array
# initializing array with array values and signed integers
arr = array.array('i', [1, 2, 3])
# printing original array
print ("The new created array is :",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
print("\r")
# using append() to insert new value at end
arr.append(4)
# printing appended array
print("The appended array is : ", end="")
for i in range (len(arr)):
print (arr[i], end=" ")
OutputThe new created array is : 1 2 3
The appended array is : 1 2 3 4
Insert() Method
insert() method is used to add a value at a specific index in an array. It takes two arguments, position where the element should be inserted and the value to insert.
Example:
It demonstrates to use insert() method to add an element at a specific position in an array. It starts by creating an array of signed integers, prints original array, then inserts value 5 at index 2. Finally, it prints updated array to show result of insertion.
Python
import array
# initializing array with array values and signed integers
arr = array.array('i', [1, 2, 3])
# printing original array
print ("The new created array is : ",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
arr.insert(2, 5)
print("\r")
# printing array after insertion
print ("The array after insertion is : ", end="")
for i in range (len(arr)):
print (arr[i], end=" ")
OutputThe new created array is : 1 2 3
The array after insertion is : 1 2 5 3
Pop() Method
pop() method is used to remove and return element at specified index in an array. If no index is given, it removes the last element by default.
Example:
This example shows how to use pop() method. It first initializes an array with integer values, then removes element at index 2 using pop(2) and prints removed element. Finally, it displays updated array after removal.
Python
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 5])
# printing original array
print ("The new created array is : ",end="")
for i in range (0,5):
print (arr[i],end=" ")
print("\r")
# using pop() to remove element at 2nd position
print ("The popped element is : ",end="")
print (arr.pop(2))
# printing array after popping
print ("The array after popping is : ",end="")
for i in range (len(arr)):
print (arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 5
The popped element is : 3
The array after popping is : 1 2 1 5
Remove() Method
remove() method is used to delete the first occurrence of a specific value from the array.
Example:
This code shows how to use remove() method to delete first occurrence of a specified value (in this case, 1). It first initializes and prints original array, then removes the value and finally prints updated array to show the result.
Python
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 5])
# printing original array
print ("The new created array is : ",end="")
for i in range (0,5):
print (arr[i],end=" ")
print("\r")
# using remove() to remove 1st occurrence of 1
arr.remove(1)
# printing array after removing
print ("The array after removing is : ",end="")
for i in range (len(arr)):
print (arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 5
The array after removing is : 2 3 1 5
Index() Method
index() method is used to find the position of the first occurrence of a given value in the array.
Example:
This code shows how to use index() method. It first creates an array of integers, prints original array and then finds the index of the first occurrence of the value 2 using arr.index(2).
Python
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
# printing original array
print ("The new created array is : ",end="")
for i in range (0,6):
print (arr[i],end=" ")
print("\r")
# using index() to print index of 1st occurrence of 2
print ("The index of 1st occurrence of 2 is : ",end="")
print (arr.index(2))
OutputThe new created array is : 1 2 3 1 2 5
The index of 1st occurrence of 2 is : 1
Reverse() Method
reverse() method is used to reverse the elements of an array in place.
Example:
This example shows how to reverse an array. It begins by creating an array with int values, then prints original array. The reverse() method is applied to invert the order of elements in-place and finally, reversed array is printed.
Python
import array
# initializing array with array values
arr= array.array('i',[1, 2, 3, 1, 2, 5])
# printing original array
print ("The new created array is : ",end="")
for i in range (0,6):
print (arr[i],end=" ")
print("\r")
#using reverse() to reverse the array
arr.reverse()
# printing array after reversing
print ("The array after reversing is : ",end="")
for i in range(len(arr)):
print (arr[i],end=" ")
OutputThe new created array is : 1 2 3 1 2 5
The array after reversing is : 5 2 1 3 2 1
Related Article
Array in Python | Set 2 (Important Functions)
Similar Reads
Array in Python | Set 2 (Important Functions) Array in Python | Set 1 (Introduction and Functions)Array in Python | Set 2Below are some more useful functions provided in Python for arrays: Array Typecode FunctionThis function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data
3 min read
Intersection() function Python Python set intersection() method returns a new set with an element that is common to all set The intersection of two given sets is the largest set, which contains all the elements that are common to both sets. The intersection of two given sets A and B is a set which consists of all the elements whi
2 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read
Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar
3 min read