Table of contents
1.
Introduction
2.
What are Arrays in Python?
3.
Representation of Array in Python
3.1.
Example:
4.
Array operations in Python
5.
Creating an array
5.1.
Example
5.2.
Python
6.
Adding Elements to a Array
6.1.
Example
6.2.
Python
7.
Accessing array elements
7.1.
Example
7.2.
Python
8.
Removing Elements from the Array
8.1.
Example
8.2.
Python
9.
Slicing of an Array
9.1.
Example
9.2.
Python
10.
Searching an element in an Array
10.1.
Example
10.2.
Python
11.
Updating Elements in an Array
11.1.
Example
11.2.
Python
12.
Frequently Asked Questions
12.1.
How can I check the length of an array in Python?
12.2.
How can I create an empty array in Python?
12.3.
How can I sort an array in Python?
12.4.
What is a typed array in Python?
12.5.
How is an array represented in Python?
12.6.
How to denote an array in Python?
13.
Conclusion
Last Updated: Feb 5, 2025
Easy

Python Arrays

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Arrays are an essential part of programming, allowing us to store and manage multiple values efficiently. In Python, arrays help organize data in a structured way, making it easier to perform operations like searching, sorting, and modifying elements.

Although Python does not have built-in support for traditional arrays like other languages (such as C or Java), it provides alternatives like the array module and lists, which serve similar purposes.

Array in python

What are Arrays in Python?

An Array in Python is a collection of items stored in a contiguous memory block. The items in an array can be of any data type, and integers index them. Arrays are commonly used to store collections of data, such as lists of numbers, strings, or other objects.

array

In Python, Arrays can be created using the built-in "array" module. The "array" class has a variety of methods and attributes that can be used to manipulate the data stored in the array. For example, the "append" method can be used to add new items to the end of the array, and the "pop" method can be used to remove an item from the end of the Array.

Representation of Array in Python

In Python, arrays can be represented using the built-in "array" module. To create an array, you first need to import the "array" module and then call the "array" function with the type code of the items you want to store in the Array, followed by an iterable containing the items. For example, to create an array of integers, you can use the following code:

Example:

from array import array


my_array = array("i", [1, 2, 3, 4, 5])

In this example, "i" is the type code for integers, and the array is initialized with the values [1, 2, 3, 4, 5]. 

The type codes that can be used with the "array" function include:

Type codePython code
"b"for signed byte
"B"for unsigned byte
"u"for Unicode char
"h"for short
"H"for unsigned short
"i"for int
"I"for unsigned int
"l"for long
"L"for unsigned long
"f"for float
"d"for double

Alternatively, the array can be represented using the numpy library, which provides more advanced array manipulation capabilities. Here is an example of creating an array of integers using numpy:

import numpy as np


my_array = np.array([1, 2, 3, 4, 5])

In this example, the "np.array" function is used to create a numpy array and it's initialized with the values [1, 2, 3, 4, 5].

Array operations in Python

There are several important array operations in Python:

  • Creating an Array: Python's array module can be used to create an array. The syntax array(data type, value list) requires a data type and a list of values.
  • Adding Elements to an Array: Elements can be added using the insert() method at a specific index or append() to add elements at the end of the array.
  • Accessing Array Elements: Elements in an array can be accessed using their index numbers inside square brackets []. Indexing starts from zero.
  • Removing Elements from an Array: The remove() method deletes a specific element, while the pop() method removes an element by index. If no index is given, pop() removes the last element.
  • Slicing an Array: The slice operation [:] is used to extract a portion of an array. It allows accessing elements within a specified range and can also be used to reverse the array.
  • Searching an Element in an Array: The index() method returns the position of the first occurrence of a specified value in the array.
  • Updating Elements in an Array: To update an element, assign a new value to the desired index in the array.

Creating an array

Python's array module can be imported to generate an array. An array can be created by using the syntax array(data type, value list), which takes two arguments: a data type and a value list.

Example

  • Python

Python

import array 

# Integer array
x = array.array('i', [1, 2, 3, 4, 5])

print ("Integer array: ", end =" ")
for i in range (0, 5):
print (x[i], end =" ")
print()

# creating an array
y = array.array('u', ['n', 'i', 'n', 'j', 'a', 's'])

print ("Character array: ", end =" ")
for i in range (0, 6):
print (y[i], end =" ")
You can also try this code with Online Python Compiler
Run Code

Output

output


You can practice by yourself with the help of online python compiler for better understanding.

Adding Elements to a Array

The Array can have elements added to it by using the built-in insert() function. The insert command can add one or more data elements to an array. At the start, end, or any specified index of the Array, a new element may be inserted depending on the necessity. The value specified in its arguments can also be added to the end of the Array using the append() function.

Example

  • Python

Python

import array 

# Integer array
x = array.array('i', [4, 5, 6])

print ("Array before insertion : ", end =" ")
for i in range (0, 3):
print (x[i], end =" ")
print()

# inserting array using insert operation
x.insert(1, 7)

print ("Array after insertion : ", end =" ")
for i in (x):
print (i, end =" ")
print()


# Character array
y = array.array('u', ['n', 'i', 'n', 'j'])

print ("Array before insertion : ", end =" ")
for i in range (0, 4):
print (y[i], end =" ")
print()

#Using append() operation add element
y.append('s')

print ("Array after insertion : ", end =" ")
for i in (y):
print (i, end =" ")
print()
You can also try this code with Online Python Compiler
Run Code

Output

output

Accessing array elements

Use the index number to get at the array's items. To access a component in an array, type the index operator []. Integers are required for the index.

Example

  • Python

Python

import array 

# Integer array
a = array.array('i', [0, 1, 2, 3, 4])
print("The access element is: ", a[1])
print("The access element is: ", a[3])

# Character element
b = array.array('u', ['n', 'i', 'n', 'j', 'a', 's'])
print("The access element is: ", b[3])
print("The access element is: ", b[5])
You can also try this code with Online Python Compiler
Run Code

Output

output

Removing Elements from the Array

Using the Array's built-in remove() function, elements can be removed; however, if the element doesn't already exist in the set, an error is raised. Iterators are used to delete a range of elements because the Remove() method only removes one element at a time. However, by default, the pop() function only removes the last member of the Array. The element's index is supplied as an argument to the pop() method to remove an element from a specific place in the Array.

Example

  • Python

Python

import array
my_arr = array.array('i', [1, 3, 5, 1, 7])


# Print the original array
print ("The new created array is : ", end ="")
for i in range (0, 5):
print (my_arr[i], end =" ")


print ("\r")


# using pop() Operation remove element at 3rd position
print ("The popped element is : ", end ="")
print (my_arr.pop(4))


# Print the element after popping
print ("The array after popping is : ", end ="")
for i in range (0, 4):
print (my_arr[i], end =" ")


print("\r")


my_arr.remove(1)


# Print the element after removing
print ("The array after removing is : ", end ="")
for i in range (0, 3):
print (my_arr[i], end =" ")
You can also try this code with Online Python Compiler
Run Code

Output

output

Slicing of an Array

There are other ways to print an array's entire contents, but we use the Slice operation to print a selected subset of the Array's elements. The Array is subjected to a slice operation using a colon (:). Use [:Index] to print elements from beginning to range, [:-Index] to print elements from range to end, [Index:] to print elements from particular Index to end, [Start Index:End Index] to print elements within range, and [:] to print entire List using slicing operation. Additionally, use [::-1] to print the entire Array in reverse order.

Example

  • Python

Python

import array 

li = ['c', 'o', 'd', 'i', 'g', 'n', 'i', 'n', 'j', 's']

x = array.array('u', li)
print("Begining Array: ")
for i in (x):
print(i, end =" ")

# Print elements of a any range
# using Slice operation
Sliced_array = x[4:8]
print("\nSliced elements in a range 4-8: ")
print(Sliced_array)

# Print elements from a
# pre-defined point to end
Sliced_array = x[6:]
print("\nElements sliced from 6th "
"element till the end: ")
print(Sliced_array)

# Print elements from
#starting to till end
Sliced_array = x[:]
print("\nPrint all elements: ")
print(Sliced_array)
You can also try this code with Online Python Compiler
Run Code

Output

output

Searching an element in an Array

We use the index() method that is built into Python to search for a specific element in the array. The index of the first time a value mentioned in parameters appears is returned by this method.

Example

  • Python

Python

import array
#Integer array
my_arr = array.array('i', [1, 2, 1, 5, 3, 5])


# print the original array
print ("The new created array is : ", end ="")
for i in range (0, 6):
print (my_arr[i], end =" ")


print ("\r")


print ("The index of 1st occurrence of 5 is : ", end ="")
print (my_arr.index(5))



print ("The index of 1st occurrence of 1 is : ", end ="")
print (my_arr.index(1))
You can also try this code with Online Python Compiler
Run Code

Output

output

Updating Elements in an Array

We just assign a new value to the target index we wish to change to update an element in the array.

Example

  • Python

Python

import array
my_arr = array.array('i', [1, 2, 5, 5, 2, 5])


# print the original array
print ("Array before update : ", end ="")
for i in range (0, 6):
print (my_arr[i], end =" ")


print ("\r")


my_arr[2] = 6
print("Array after update : ", end ="")
for i in range (0, 6):
print (my_arr[i], end =" ")
print()


# update the element in a array
my_arr[4] = 8
print("Array after updation : ", end ="")
for i in range (0, 6):
print (my_arr[i], end =" ")
You can also try this code with Online Python Compiler
Run Code

Output

output

Check out this array problem - Merge 2 Sorted Arrays

Must Read Python List Operations

Frequently Asked Questions

How can I check the length of an array in Python?

You can use the built-in "len" function to check the length of an array. For example, to check the length of the array "my_array", you can use the following code:

len(my_array).

How can I create an empty array in Python?

You can create an empty array in Python using the "array" function with no initial values. For example, to create an empty array of integers, you can use the following code:

from array import array

my_array = array("i")

How can I sort an array in Python?

You can use the built-in "sorted" function to sort an array. For example, to sort the array "my_array", you can use the following code:

sorted_array = sorted(my_array)

What is a typed array in Python?

A typed array in Python is an array where all elements must be of the same data type, specified during its creation using the array module.

How is an array represented in Python?

In Python, arrays are represented using the array module or lists, which are collections of elements indexed by integers.

How to denote an array in Python?

Arrays in Python are denoted using the array module with the syntax array(typecode, values), or by using a list [] for dynamic arrays.

Conclusion

In this article, we have extensively discussed the details of Array in Python, in which we talk about searching, slicing, updating, and removing elements.

We hope the blog has helped you enhance your knowledge regarding Array in Python

Recommended problems -

Live masterclass