×


Python Arrays in Linux - Explained with examples

An array is a collection of elements of the same data type stored in adjacent memory locations. Arrays make it easy to calculate the positions of elements.

They are useful when we only have to work with a specific data type value. The user can treat the list as an array and note the data type in the array must be the same. 

Here at Ibmi Media, we shall look into how to use Array in Python.


Basic example of Python Arrays function

For instance, take a look at:

animals = ["cat", "dog", "tiger"]
print(animals)

The output will give:

[‘cat’, ‘dog’, ‘tiger’]


1. How to access array elements ?

Now, we will access the first item:

animals = ["cat", "dog", "tiger"]
x = animals[0]
print(x)

The Output will give:

cat

For example, we will edit the value of the first item:

animals = ["cat", "dog", "tiger"]
animals[0] = "chicken"
print(animals)

Output will give:

[‘chicken’, ‘dog’, ‘tiger’]


2. The length of an array

Use len() function to print out the length of the array. For example:

animals = ["cat", "dog", "tiger"]
x = len(animals)
print(x)

Output will give:

3


3. Loop array elements

Using for in loop to iterate over each element of the array. For example, we will print out each item in the animals array:

animals = ["cat", "dog", "tiger"]
for x in animals:
print(x)

Output will give:

cat
dog
tiger


4. Removing Python Array Elements

We can delete one or more items from an array using Python's del statement:

import array as arr
number = arr.array('i', [1, 2, 3, 3, 4])
del number[2]  # removing third element
print(number)  # Output: array('i', [1, 2, 3, 4])
del number  # deleting entire array
print(number)  # Error: array is not defined

Output will give:

array('i', [1, 2, 3, 4])
Traceback (most recent call last):
  File "<string>", line 9, in <module>
    print(number)  # Error: array is not defined
NameError: name 'number' is not defined

We can use the remove() method to remove the given item, and pop() method to remove an item at the given index:

import array as arr
numbers = arr.array('i', [10, 11, 12, 12, 13])
numbers.remove(12)
print(numbers)   # Output: array('i', [10, 11, 12, 13])
print(numbers.pop(2))   # Output: 12
print(numbers)   # Output: array('i', [10, 11, 13])

Output will give:

array('i', [10, 11, 12, 13])
12
array('i', [10, 11, 13])


[Need Python Expert support ? We can help you. ]


Conclusion

This article covers how to use Array in Python. In fact, An array is a special variable, which can hold more than one value at a time.


Python Lists Vs Arrays

In Python, we can treat lists as arrays. However, we cannot constrain the type of elements stored in a list. 

For example:

# elements of different types
a = [1, 3.5, "Hello"] 
If you create arrays using the array module, all elements of the array must be of the same numeric type.
import array as arr
# Error
a = arr.array('d', [1, 3.5, "Hello"])

Output will give:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
    a = arr.array('d', [1, 3.5, "Hello"])
TypeError: must be real number, not str