bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Arrays in Python

An array is a data structure that can contain or hold a fixed number of elements that are of the same datatype. An array is composed of an element and an index. Index in an array is the location where the element resides. All elements have their respective indices. Index of an array always starts with 0. The following diagram illustrates the array representation.

Unlike other programming languages such as Java, C, C++ and more, arrays are not that popular in Python since there are many iterable data types in Python that are flexible and fast to use such as Python Lists. However, Python arrays are still used in certain cases. We will learn all about arrays, from what they are and when they are used in this module.

Following is the list of topics that we will cover in this Python arrays module, in case you want to jump to a specific topic.

So without any further delay, let’s get started.

Arrays vs lists in Python

The basic difference between array and lists in Python is that the lists are flexible and can hold completely arbitrary data of any datatype while arrays can only hold data of the same datatype.

Also, the array is considered useful in terms of memory efficiency but it is usually slower than lists. As mentioned above, the Python array module is not that popular to use but they do get used in certain cases such as:

  • Array modules are used for interfacing with C code.
  • When you need to allocate a fixed length array that will not change as using array, in this case, will be faster than lists.

Creating an array

In Python, the array module supports numeric arrays. So to create an array in Python we will have to import the array module. Following is the syntax to create an array:

from array import *
arraname = array(typecode, [Initializers])

Here typecode is what we use to define the type of the value that is going to be stored in the array. Some of the common typecodes used in the creation of arrays in Python are described in the following table.

Commonly used type codes:

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
‘f’ float float 4
‘d’ double float 8

Now let’s create an array using the above-mentioned syntax.

Example:

import array as arr
a = arr.array(‘I’, [2,4,6,8])
print(a)

Output:

array(‘I’, [2, 4, 6, 8])

Data science masters program

Accessing an array element

We can access the elements of an array in Python using the respective index of the elements, as shown in the following example.

from array import*
array_1 = array(‘i’, [1,2,3,4,5])
print (array_1[0])
print (array_1[3])
1
4

The index of the array elements start from 0, when we printed the value of array1[0], it displayed the first element.

Basic Operations in arrays

Following listed are some of the basic operations supported by array module in Python.

1. Traverse: iterating between elements in an array is known as Traversing. We can easily iterate through the elements of an array using for loop as shown in the example below.

Example:

from array import *
array_1 = array(‘i’, [1,2,3,4,5])
for x in array_1:
print (x)

Output:

1
2
3
4
5

2. Insertion: Using this operation we can add one or more elements at any given index.

Example:

from array import *
array_1 = array(‘i’, [1,2,3,4,5])
array_1.insert(1,6)
for x in array_1:
print (x)

Output:

1
6
2
3
4
5

3. Deletion: Using this operation we can delete any element residing at the specified index. We can remove any element using the inbuilt remove() method.

Example:

from array import *
array_1 = array(‘i’, [1,2,3,4,5])
array_1.remove(2)
For x in array_1:
print (x)

Output:

1
3
4
5

4. Search: Using this operation we can search for an element using its index or its value.

Example:

from array import *
array_1 = array(‘i’, [1,2,3,4,5])
print (array_1.index(3))

Output:

2

In the above example, we have searched for the element using the built-in index() method. Using index(3) gave the output 2 which means that 3 is at index number 2 in the array_1. If the searched value is not present in the array then the program will return an error.

5. Update: Using this operation we can update an element at the given index.

Example:

from array import *
array_1 = array(‘i’, [1,2,3,4,5])
array_1[2] = 100
for x in array_1:
print(x)

Output:

1
2
100
4
5

In the above example, we have updated the already existing value at index 2 instead of adding a new element.

With this, we have come to the end of this module. The next module will cover Python classes and objects. See you there!

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python Arrays"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Arrays.