Introduction to Arrays in Python

Favour Kelvin
Nerd For Tech
Published in
4 min readOct 19, 2021

--

Photo by Mohammad Rahmani on Unsplash

Arrays in Python

In Python, arrays allow you to store many values of the same type. The above lines are often misinterpreted as lists; however, python arrays are not the same as lists. Let’s take a look at what arrays are and how they can be implemented.

What are Arrays

A static data structure in computer programming used to hold data of the same kind is known as an array.

An array is the most important kind of data structure in Python for data science and can be used for a number of purposes such as implementing other data structures like a stack or a queue.

An important note when using an array is that every element must be of the same type, such as all integers or all floats, etc.

Advantages of using an Array

Advantages of an array data structure are:

  • Arrays are capable of effectively managing extremely big datasets.
  • Calculations and analysis are faster in arrays
  • It’s used to represent many data elements of the same type with a single name.
  • It is capable of implementing more data structures such as linked lists, stacks, queues, trees, and graphs.
  • Matrixes are represented using two-dimensional arrays.

Disadvantages of using an Array

Disadvantages of an array data structure are:

  • The array’s size cannot be changed once it is defined. It has no way of increasing or decreasing the memory assigned to it.
  • Inserting and deleting items in an array is very tough due to the elements being stored in consecutive memory regions.
  • The number of items to be stored in the array must be known in advance.
  • Due to the finite size of an array, if we allocate more memory than is required, we will waste memory space. And if we allocate less RAM than is required, a problem will arise.

Applications of Arrays

Apart from being widely used in programming, arrays have additional applications, it can be used to:

  • Stores data elements of the same data type.
  • Maintains multiple variable names using a single name.
  • Sort data elements using different sorting techniques like the bubble sort, insertion sort, selection sort, etc
  • Perform matrix operations.
  • Implement other data structures like stacks, queues, heaps, hash tables, etc.

Array Examples

To demonstrate how arrays work, we will convert alistto an arrayusing the array() function from NumPy.

To do this, first, we create an integer-based list and then use the array() function to convert it to an array. Copy and paste the following

import numpy as npinteger_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]to_array = np.array(integer_list)
print(to_array)

The output: ([1, 2, 3, 4, 5, 6, 7, 8, 9])

Access Elements in an Array

Elements in an array are referred to as index numbers.

Now, to get the value of the first array item:

names = ["Favour", "Kelvin", "Mary", "Jane"]z = names[0]print(z)

The output: Favour

Length of an Array

The length of an array refers to the number of elements in an array.

To find the length of an array we use the len() method

names = ["Favour", "Kelvin", "Mary", "Jane"]z = len(names)print(z)

The output: 4

Looping Elements in an Array

To loop through all the elements in an array we use the for in loop

names = ["Favour", "Kelvin", "Mary", "Jane"]for z in names:
print(z)

The output:

Favour
Kelvin
Mary
Jane

Adding Elements in an Array

To add an element in an array we use the append() method

names = ["Favour", "Kelvin", "Mary", "Jane"]names.append("John")print(names)

The output: [‘Favour’, ‘Kelvin’, ‘Mary’, ‘Jane’, ‘John’]

Removing Elements in Array

To remove an element in an array we use the remove() method.

names = ["Favour", "Kelvin", "Mary", "Jane"]names.remove("Jane")print(names)

The output: [‘Favour’, ‘Kelvin’, ‘Mary’]

This method removes only the first occurrence of the specified value. For example

names = ["Favour", "Jane", "Kelvin", "Mary", "Jane"]names.remove("Jane")print(names)

The output: [‘Favour’, ‘Kelvin’, ‘Mary’, ‘Jane’]

Alternatively, you can also use the pop() method to remove an element from an array by specifying the index.

names = ["Favour", "Kelvin", "Mary", "Jane"]names.pop(1)print(names)

The output: [‘Favour’, ‘Mary’, ‘Jane’]

Another example

names = ["Favour", "Kelvin", "Mary", "Jane"]names.pop(1)print(names)

The output: [‘Kelvin’, ‘Mary’, ‘Jane’]

Conclusion

Yaay!!🥳 you were able to make it to the end of this article.

I hope this provides a decent introduction to the array data structure in Python. Best wishes, and have fun learning!

--

--

Favour Kelvin
Nerd For Tech

Software engineer, Technical writer. I enjoy the synergy of writing and technology