Python Array (with examples)

Python array

Hello Techies,

This blog is about “Python Array”. An array is a container that stores multiple items of the same data type. So in this blog, we will learn about the usage of the Array in Python with the help of multiple examples.

Below are the key points of Python Array that we covering in this blog:

  1. Python Array(Introduction)
  2. Create Arrays in Python
  3. Accessing Python Array Element
  4. Add and Change Python Array Element
  5. Delete or Remove elements from Python Array
  6. Python Array Slice
  7. Python Array Methods

#1. Python Array(Introduction)

Python arrays are a collection of common types of data structures that contain elements of the same data type. In Python programming, arrays are handled by the “Numpy Package” and “Array” modules.

The Array has several important features:

  • Array size not specified in Python. Arrays can dynamically increase or decrease their size.
  • An array is a collection of common types of data structures that contain elements of the same data type.
  • Elements can be easily accessed in an array using an index number.

#2. Create Arrays in Python

In Python, we use the “Numpy Package” and “Array” modules to create arrays. Let us check the examples using the Numpy package and Array module.

Example 1: How to create an array using the Array module?

import array as arr
data = arr.array('i', [5, 4, 6, 7])

Output is:

array('i', [5, 4, 6, 7]) 

The above code creates arrays for integer-type elements. The letter ‘i‘ is a type code for an integer data type used to create an integer array.

The most commonly used types code are listed below:

Type codePython typeC TypeMin size(bytes)
‘b’IntSigned char1
‘B’IntUnsigned char1
‘d’FloatDouble8
‘f’FloatFloat4
‘h’IntSigned short2
‘H’IntUnsigned short2
‘i’IntSigned int2
‘I’IntUnsigned int2
‘l’IntSigned long4
‘L’IntUnsigned long4
‘q’IntSigned long long8
‘Q’IntUnsigned long long8
‘u’Unicode characterPy_UNICODE2

Example 2: How do you create a Numpy array in Python??

To create an array using the NumPy library, you must first install the NumPy package in your virtual environment. Check out the example below.

import numpy as np

arr = np.array([5, 4, 6, 7])
print(arr)
print(type(arr))

Output is:

[5, 4, 6, 7]
<class 'numpy.ndarray'>

Please see the official link for more information about the Numpy package.


#3. Accessing Python Array Element

We use indexes to access the elements of the array.

Syntax for accessing the python array element is:

array_name[index_number] 

We can access the Python array using positive indexing and negative indexing.

Example 1: Positive Indexing: How to access Python array elements?

import array
data = array.array('i', [4, 5, 6, 7])
print(data[1])

Output is:

5

Example 2: Negative Indexing: How to access Python array elements?

import array
data = array.array('i', [4, 5, 6, 7])
print(data[-1])

Output is:

7

#4. Add and Change Python Array Element

The arrays are mutable so we can modify its element.

We can use the append() method to add any new elements to the array or add several elements using the extend() method.

import array
data = array.array('i', [4, 5, 6, 7])
data.append(2)
print(data)

data.extend([1, 9, 8])
print(data)

Output is:

array('i', [4, 5, 6, 7, 2])  
array('i', [4, 5, 6, 7, 2, 1, 9, 8])  

We can use the = operator to modify any element in the array.

import array
data = array.array('i', [4, 5, 6, 7])
data[0] = 2
print(data)

Output is:

array('i', [2, 5, 6, 7]) 

#5. Delete or Remove elements from Python Array

To delete one or more elements from the array using Keyword del. It may even delete the entire array.

import array
data = array.array('i', [4, 5, 6, 7])

# deleting the single element
del data[2]
print(data)

# deleting multiple elements
del data[1:5]
print(data)

# deleting the entire array
del data
print(data)

Output is:

array('i', [4, 5, 7]) 
array('i', [4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined

We can use the remove() method to remove a given item from an array or the pop() method to remove an item from a given index.

The pop() method removes and returns the last item if no index is provided.

import array
data = array.array('i', [4, 5, 6, 7])

data.remove(6)
print(data)

print(data.pop(1))
print(data)

print(data.pop())
print(data)

Output is:

array('i', [4, 5, 7])
5
7
array('i', [4])  

#6. Python Array Slice

Slice Operator: Using (colon) we can enter the range of items in the array.

import array
data = array.array('i', [4, 5, 6, 7])

# elements 2nd to 5th
print(data[1:5])

# elements beginning to 6th
print(data[:-3])

# elements 3rd to end
print(data[2:])

# elements beginning to end
print(data[:])

Output is:

array('i', [5, 6, 7])
array('i', [4])
array('i', [6, 7])
array('i', [4, 5, 6, 7])

FAQ on Python Array

  1. Is array available in Python?

    Python has no built-in support for the array, but you can create Python arrays using the “Numpy Package” and “Array” modules.

  2. What is difference between Array and List in Python?

    The main difference between a Python list and an array is that the list is part of Python’s standard package, while the array requires the import of the “NumPy Package” and “Array” modules.

  3. Is list in Python same as array?

    In Python, both arrays and lists are used to store data, and both data structures allow indexing, slicing, and iteration. However, there are some significant differences between these two data structures, such as arrays requiring declarations while the list does not require declarations because Python has built-in support to the list. For this reason, lists are used more often than arrays.

Leave a Comment