Arrays
In this lesson, we learn about arrays and how they are used in Python!
Introduction
In Python, an array is just an ordered sequence of homogeneous elements. In other words, an array can only hold elements of one datatype. Python arrays are basically just wrappers for C arrays. The type is constrained and specified at the time of creation.
Initializing Arrays
Python arrays are initialized using the array library:
import array
new_array = array.array('type', [list])
Here type
defines the data type of array and list
is a python list containing homogenous elements.
Consider the example below:
Press + to interact
import array# type: 'f' (float), initializer list: [1, 2, 3]new_array = array.array('f', [1, 2, 3])print(new_array[0])
Line 4 creates an array. Here f
indicates that the array is of type float
.
Types of Arrays
There are several types of arrays in Python; refer to ...