The official Python documentation contains information about arrays. However, in Python, the built-in data structure commonly used to represent arrays is the list. The official documentation primarily focuses on lists rather than a separate array data type. Lists are ordered, mutable, and can contain elements of different data types. You can access elements in a list using their indices, and you can perform various operations on lists such as appending, inserting, or removing elements. With these facts in mind, we'll look at lists from, say, an array's point of view.
Python's Array
Let's look at what an array is in Python. An array is a data structure that stores a collection of elements of the same type. It is a container that holds a fixed number of items, and the elements can be accessed using their indices. Python provides several ways to work with arrays, including built-in data structures like lists and the NumPy library's ndarray.
There are several possibilities how to make an array in Python.
As we already mentioned, lists are usually used as arrays in Python. But if you want to improve performance and reduce memory consumption for certain use cases, you can use the array
module in Python. It provides a way to create arrays that are more efficient and compact compared to traditional lists, it allows you to define arrays of a specific data type.
To use the array
module, you first need to import it:
import array
Next, you can create an array in Python by specifying the desired data type code and initializing it with values. Here's an example of creating an array of integers:
my_array = array.array('i', [1, 2, 3, 4, 5])
In the above example, 'i'
represents the data type code for signed integers. You can choose different data type codes based on your specific needs (e.g., 'f'
for floats, 'd'
for doubles, 'b'
for bytes, etc.).
Once you have created an array, you can access its elements using indexing, just like with regular lists. You can also modify the values in the array or perform various operations available for arrays.
Note: the
array
module is particularly useful when you are working with large amounts of numerical data or when you need to interface with low-level libraries that expect data in a specific format. For general-purpose collections of heterogeneous elements, the built-in list type is usually more flexible and commonly used in Python.
Array vs List in Python
In Python, the terms "array" and "list" are often used interchangeably, but they refer to different data structures with some distinctions. Let's explore the differences between them:
-
Memory Allocation: Arrays in Python are provided by the
array
module and represent a fixed-size, homogeneous collection of elements. They are generally more memory-efficient compared to lists because they store elements of the same type contiguously in memory. Lists, on the other hand, are heterogeneous and can store elements of different types. Lists are implemented as dynamic arrays that automatically resize themselves to accommodate new elements. -
Data Types: Arrays are constrained to a specific data type. When creating an array, you need to specify the type of elements it will hold (e.g., integers, floats, characters). This constraint allows arrays to provide more efficient storage and operations on their elements. Lists, being heterogeneous, can contain elements of different data types within the same list.
-
Flexibility: Lists are more flexible compared to arrays. They can grow or shrink dynamically, as elements can be added or removed at any position. Arrays, once created, have a fixed size and cannot be changed. If you need to modify an array's size, you would have to create a new array with the desired size and copy the elements from the old array.
-
Operations and Methods: Both arrays and lists provide common operations like indexing, slicing, and iteration. However, arrays have additional methods provided by the
array
module, such as efficient mathematical operations on the array as a whole (e.g., sum, product), which can be faster compared to equivalent operations on lists. External Libraries: Some external libraries, such as NumPy, provide multidimensional arrays that are widely used for numerical computations. NumPy arrays offer efficient storage and vectorized operations on arrays, making them highly optimized for numerical computations. Lists do not have such built-in functionality.
In summary, arrays are fixed-size, homogeneous collections of elements that are memory-efficient and provide specific operations, while lists are dynamic, heterogeneous collections that offer more flexibility and versatility. The choice between arrays and lists depends on the specific requirements of your program, such as memory usage, data type constraints, and the need for dynamic resizing or specialized operations.
How to Create an Array
In Python, arrays can be created using various methods and libraries. There are also some other parameters which should be taken into account at the moment of array creation.
Simple Array with Integers
You can create an array in Python using the built-in array
module or by simply initializing an empty list. Here are two examples of creating arrays:
- Initializing an array of integers using the
array
module:
import array
# Create an array of integers
my_array = array.array('i', [1, 2, 3, 4, 5])
- The second approach is to declare a list instead of an array:
# Create a list
my_list = [1, 2, 3, 4, 5]
To create an empty array, you can follow the approaches mentioned above. Next, we'll look at the defining of an array of size n.
Array of Size N
To create an array of a specific size in Python, you can use various methods, including using a list comprehension or using NumPy. Here are a few examples of arrays' declaring:
Using a list comprehension:
n = 5
my_array = [0] * n
print(my_array) # Output: [0, 0, 0, 0, 0]
Using NumPy:
import numpy as np
n = 5
my_array = np.zeros(n)
print(my_array) # Output: [0. 0. 0. 0. 0.]
Random-Generated Array
To generate a random array in Python, you can use the random module
from the Python standard library or the numpy library. Here are examples using both approaches:
Using the random module:
import random
# Generate a random array of size n
n = 5
random_array = [random.randint(0, 100) for _ in range(n)]
print(random_array) # Output: [47, 92, 32, 12, 80]
Using the numpy library:
import numpy as np
# Generate a random array of size n
n = 5
random_array = np.random.randint(0, 100, size=n)
print(random_array) # Output: [92 76 32 48 11]
Both approaches allow you to generate random arrays of integers. Adjust the parameters (a
, b
, and size
) based on your specific requirements to control the range and size of the random array.
2D Array in Python
Here is an example how to initialize a multi-dimensional array in Python using np.array()
function:
import numpy as np
# Create a 2-dimensional array
arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr2)
# Output:
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
You can also create a two-dimensional array using a list of lists, where each inner list represents a row. Here's an example of how to create and initialize a 2D array using nested lists:
# Create a 2D array with 3 rows and 4 columns
array_2d = [[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]]
# Accessing elements
print(array_2d[0][2]) # Output: 2 (element at row 0, column 2)
print(array_2d[1][3]) # Output: 7 (element at row 1, column 3)
# Modifying elements
array_2d[2][1] = 99 # Changing element at row 2, column 1 to 99
print(array_2d[2][1]) # Output: 99
# Iterating through the array
for row in array_2d:
for element in row:
print(element, end=' ')
print()
How to Create a NumPy Array in Python
To create a NumPy array in Python, you can use the numpy.array()
function. Here's an example of np
array initialization:
import numpy as np
# Create a 1-dimensional array
arr1 = np.array([1, 2, 3, 4, 5])
print(arr1)
# Output: [1 2 3 4 5]
In the above code, import numpy as np
imports the NumPy module, allowing us to use its functions and classes.
Array of Strings in Python
To create an array of strings in Python, you can use a list where each element of the list represents a string. Here's an example:
array = ['apple', 'banana', 'orange', 'grape']
print(array) # Output: ['apple', 'banana', 'orange', 'grape']
In the above example, we create an array of strings called array
using a list. Each element of the list represents a string. The resulting array contains four strings: 'apple', 'banana', 'orange', and 'grape'.
Array of Dictionaries
In Python, you can create an array (or list) of dictionaries by simply initializing a list and adding dictionaries as its elements. Each dictionary can contain key-value pairs representing different properties or attributes. Here's an example:
# Create an array of dictionaries
array_of_dictionaries = [
{"name": "John", "age": 25, "city": "New York"},
{"name": "Alice", "age": 30, "city": "London"},
{"name": "Bob", "age": 35, "city": "Paris"}
]
# Accessing values
print(array_of_dictionaries[0]["name"]) # Output: John
print(array_of_dictionaries[1]["age"]) # Output: 30
print(array_of_dictionaries[2]["city"]) # Output: Paris
Array of Tuples in Python
In Python, you can create an array of tuples using different data structures. Here are a few examples:
- List of Tuples:
You can create an array of tuples using a list. Each tuple represents an element in the array. Here's an example:
# List of tuples
array = [(1, 'apple'), (2, 'banana'), (3, 'orange')]
# Accessing elements in the array
for item in array:
number, fruit = item
print(f"Number: {number}, Fruit: {fruit}")
- NumPy Array of Tuples:
If you are working with NumPy arrays, you can create an array of tuples using the np.array()
function. Here's an example:
import numpy as np
# NumPy array of tuples
array = np.array([(1, 'apple'), (2, 'banana'), (3, 'orange')])
# Accessing elements in the array
for item in array:
number, fruit = item
print(f"Number: {number}, Fruit: {fruit}")
- Array module:
If you are using the built-in array
module, you can create an array of tuples using the array
constructor. Here's an example:
import array as arr
# Array of tuples
array = arr.array('i', [(1, 2), (3, 4), (5, 6)])
# Accessing elements in the array
for item in array:
number1, number2 = item
print(f"Number 1: {number1}, Number 2: {number2}")
Array of Bytes
In Python, you can create an array of bytes using the built-in bytearray
or bytes
types. Here's an example of creating and working with an array of bytes:
Using bytearray
:
my_array = bytearray([0x41, 0x42, 0x43, 0x44]) # Creating a bytearray from a list of byte values
print(my_array) # Output: bytearray(b'ABCD')
# Accessing individual bytes
print(my_array[0]) # Output: 65
print(hex(my_array[1])) # Output: 0x42
# Modifying bytes
my_array[2] = 0x45
print(my_array) # Output: bytearray(b'ABED')
Using bytes
:
my_array = bytes([0x41, 0x42, 0x43, 0x44]) # Creating a bytes object from a list of byte values
print(my_array) # Output: b'ABCD'
# Accessing individual bytes
print(my_array[0]) # Output: 65
print(hex(my_array[1])) # Output: 0x42
Both bytearray
and bytes
represent sequences of bytes and can be used interchangeably in many contexts. Choose the appropriate one based on whether you need a mutable or immutable sequence of bytes.
The range()
Function for Array in Python
In Python, you can create an array or list of numbers using the range()
function. The range()
function generates a sequence of numbers within a specified range.
Here are a few examples of using the range()
function to create arrays or lists of numbers:
- Creating a range of numbers as a list:
my_list = list(range(5))
print(my_list)
# Output: [0, 1, 2, 3, 4]
- Creating a range of numbers with a specified start and end:
my_list = list(range(2, 10))
print(my_list)
# Output: [2, 3, 4, 5, 6, 7, 8, 9]
- Creating a range of numbers with a specified start, end, and step size:
my_list = list(range(1, 11, 2))
print(my_list)
# Output: [1, 3, 5, 7, 9]
The range()
function can be used to create arrays or lists of numbers based on different start, end, and step size values. By converting the range object to a list using the list()
function, you can obtain a list representation of the range.
Array of Zeros
In Python, you can create an array of zeros using various libraries and data structures. Here are a few examples:
- NumPy:
If you have NumPy installed, you can use the zeros()
function from the NumPy library to create an array of zeros. Here's an example:
import numpy as np
# Create a 1-dimensional array of 5 zeros
zeros_array = np.zeros(5)
print(zeros_array)
You can also create multi-dimensional arrays of zeros by specifying the shape as a tuple. For example:
import numpy as np
# Create a 2-dimensional array of 3 rows and 4 columns, filled with zeros
zeros_array = np.zeros((3, 4))
print(zeros_array)
- List comprehension:
If you prefer working with lists, you can use list comprehension to create an array of zeros. Here's an example:
# Create a 1-dimensional array of 5 zeros using list comprehension
zeros_array = [0] * 5
print(zeros_array)
For multi-dimensional arrays, you can nest list comprehensions. Here's an example:
# Create a 2-dimensional array of 3 rows and 4 columns, filled with zeros using nested list comprehension
zeros_array = [[0] * 4 for _ in range(3)]
print(zeros_array)
A Length of an Array in Python
You can get the length of an array (or any sequence) using the len()
function. The len()
function returns the number of elements in the sequence.
Here's an example of how to use len()
to get the length of an array:
my_array = [1, 2, 3, 4, 5]
length = len(my_array)
print(length) # Output: 5
In this example, len(my_array)
counts array elements and returns the length of the my_array
list, which is 5. The length
variable stores this value, and it is then printed to the console.
Note: The
len()
function works not only with arrays but with any iterable object, such as lists, tuples, strings, or sets.
Array Indexing in Python
In Python, an indexed array is typically represented using a list. The indices of a list are used to access and manipulate the elements within it, so you can access individual elements of an array (or list) using indexing. Array indexing allows you to retrieve a specific element from the array by referring to its position or index within the array.
Array indexes start at 0, so the first element of an array is at index 0, the second element is at index 1, and so on.
Here's an example of how to use array indexing in Python:
my_array = [10, 20, 30, 40, 50]
element = my_array[2]
print(element) # Output: 30
In this example, my_array[2]
retrieves the element at index 2 of my_array
, which is 30. The value is then stored in the element variable and printed to the console.
You can also use negative indexing to access elements from the end of the array. With negative indexing, -1 refers to the last element, -2 refers to the second-to-last element, and so on.
my_array = [10, 20, 30, 40, 50]
element = my_array[-1]
print(element) # Output: 50
In this case, my_array[-1]
retrieves the last element of my_array
, which is 50. The value is stored in the element variable and printed to the console.
You can also use indexing to modify the value of an element or to extract a subset of elements from an array using slicing.
Iterating Over an Array Using "for" Loop
In Python, you can use a "for" loop to iterate over the elements of an array and perform operations on each element. There are different ways to iterate over an array, depending on the type of array you are working with. Here are a few examples of looping through arrays:
- Using a for loop with a standard Python list:
my_list = [1, 2, 3, 4, 5]
for element in my_list:
print(element)
Output:
1
2
3
4
5
- Using a
for
loop with a NumPy array:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
for element in my_array:
print(element)
Output:
1
2
3
4
5
- Using a
for
loop with a multidimensional NumPy array:
import numpy as np
my_array = np.array([[1, 2], [3, 4], [5, 6]])
for row in my_array:
for element in row:
print(element)
Output:
1
2
3
4
5
6
Python Array or DataFrame
We have already seen what an array is, let's look at DataFrame.
A DataFrame (pandas) is a two-dimensional tabular data structure provided by the pandas library. It is highly versatile and widely used for data manipulation and analysis tasks. DataFrames can hold data of different types (e.g., integers, floats, strings) and provide powerful indexing, slicing, grouping, and aggregation functionalities. DataFrames are particularly useful when working with large datasets, performing complex operations, or when you need to work with labeled or structured data.
Here's an example of creating a DataFrame:
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'Country': ['USA', 'Canada', 'UK']}
df = pd.DataFrame(data)
print(df)
Output:
Name Age Country
0 John 25 USA
1 Alice 30 Canada
2 Bob 35 UK
In this example, we create a DataFrame df
using a dictionary data
and then print the resulting DataFrame.
DataFrames offer many features, such as indexing, filtering, merging, and handling missing values, making them a popular choice for data analysis and manipulation tasks.
In summary, if you need a simple data structure for basic numerical computations, a Python array can be sufficient. However, if you require more advanced data manipulation, analysis, and a tabular structure, a DataFrame (such as pandas DataFrame) would be a better choice.
How to Print an Array in Python
To print an array in Python, you can use the print()
function. The specific syntax will depend on the type of array you are working with. Here are a few examples:
- Printing a standard Python list:
my_list = [1, 2, 3, 4, 5]
print(my_list) # Output: [1, 2, 3, 4, 5]
- Printing a NumPy array:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
print(my_array) # Output: [1 2 3 4 5]
- Printing a multidimensional NumPy array:
import numpy as np
my_array = np.array([[1, 2, 3], [4, 5, 6]])
print(my_array)
Output:
[[1 2 3]
[4 5 6]]
How to Find An Element in an Array
To find an element in an array in Python, you can use various methods depending on the type of array you are working with. Here are a few examples:
- List:
If you have a standard Python list, you can use the in
operator or the index()
method to find an element:
my_list = [1, 2, 3, 4, 5]
# Using the 'in' operator
if 3 in my_list:
print("Element found")
# Using the 'index()' method
try:
index = my_list.index(3)
print("Element found at index:", index)
except ValueError:
print("Element not found")
- NumPy array:
For a NumPy array, you can use boolean indexing or the where()
function to find the indices or values that match a condition:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
# Using boolean indexing
mask = my_array == 3
indices = np.where(mask)[0]
if indices.size > 0:
print("Element found at indices:", indices)
else:
print("Element not found")
# Using 'where()' function
indices = np.where(my_array == 3)[0]
if indices.size > 0:
print("Element found at indices:", indices)
else:
print("Element not found")
The map() Function
In Python, you can use the map()
function to apply a given function to each element of an array or iterable. The map(
) function returns an iterator that contains the results of applying the provided function to each element. Here's an example of how to use map()
with an array:
my_array = [1, 2, 3, 4, 5]
# Define a function to apply to each element
def square(x):
return x ** 2
# Apply the square function to each element of the array using map()
result = map(square, my_array)
# Convert the result to a list
result_list = list(result)
print(result_list) # Output: [1, 4, 9, 16, 25]
In this example, the map(
) function is used to apply the square()
function to each element of the my_array
. The square()
function squares each input number, and the map()
function returns an iterator containing the squared values. Finally, the result is converted to a list using the list()
function.
Alternatively, you can use a lambda function with map()
to achieve the same result in a more concise way:
my_array = [1, 2, 3, 4, 5]
# Apply the lambda function to each element of the array using map()
result = map(lambda x: x ** 2, my_array)
result_list = list(result)
print(result_list) # Output: [1, 4, 9, 16, 25]
In this case, the lambda function lambda x: x ** 2
is used to square each element of the array.
The map()
function is a useful tool for applying a function to every element of an array or iterable in Python. It simplifies the process of transforming the elements and provides a concise way to perform element-wise operations.
Working with JSON Arrays
In Python, you can work with JSON arrays using the json
module, which provides functions for working with JSON data. Here's an example of how to work with a JSON array in Python:
import json
# JSON array as a string
json_data = '[{"name": "John", "age": 30}, {"name": "Alice", "age": 25}, {"name": "Bob", "age": 35}]'
# Parse the JSON array string into a Python list
array = json.loads(json_data)
# Accessing elements in the array
for item in array:
name = item['name']
age = item['age']
print(f"Name: {name}, Age: {age}")
You can also convert a Python list into a JSON array using the json.dumps()
function. Here's an example:
import json
# Python list
array = [
{"name": "John", "age": 30},
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 35}
]
# Convert the Python list to a JSON array string
json_data = json.dumps(array)
print(json_data)
How to Get the Last Element of an Array in Python
To get the last element of an array in Python, you can use indexing or built-in functions depending on the data structure you are working with. Here are a few approaches:
- List:
If you have a list, you can use negative indexing to access the last element. Here's an example:
my_list = [1, 2, 3, 4, 5]
last_element = my_list[-1]
print(last_element) # Output: 5
- NumPy array:
If you are working with a NumPy array, you can use the [-1] index to access the last element. Here's an example:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
last_element = my_array[-1]
print(last_element) # Output: 5
- Array module:
If you are using the built-in array module, you can use indexing to access the last element. Here's an example:
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
last_element = my_array[-1]
print(last_element) # Output: 5
How to Save a NumPy Array in Python
To save a NumPy array in Python, you can use the numpy.save()
function or the numpy.savez()
function. Here's how you can use each of them:
-
numpy.save()
: This function saves a single NumPy array to a binary file with a.npy
extension. You can specify the filename along with the array you want to save. Here's an example:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])
np.save('my_array.npy', my_array)
-
numpy.savez()
: This function saves multiple NumPy arrays into a single compressed.npz
file. You can provide a filename and pass the arrays as arguments. Here's an example:
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
np.savez('my_arrays.npz', array1=array1, array2=array2)
bitarray Library
In Python, you can use the bitarray
library to work with bit arrays. The bitarray
library provides a flexible and efficient way to manipulate arrays of boolean values, where each boolean value represents a single bit.
To use the bitarray
library, you first need to install it. You can install it using pip
by running the following command:
pip install bitarray
Once installed, you can start working with bit arrays using the bitarray class from the library. Here's an example:
import bitarray
# Create a new bit array with a specified length
my_bitarray = bitarray.bitarray(10)
# Set individual bits
my_bitarray[0] = True
my_bitarray[1] = False
my_bitarray[2] = True
# Access individual bits
print(my_bitarray[0]) # Output: True
print(my_bitarray[1]) # Output: False
print(my_bitarray[2]) # Output: True
# Set multiple bits at once
my_bitarray[3:6] = bitarray.bitarray([True, False, True])
# Access multiple bits at once
print(my_bitarray[3:6]) # Output: bitarray('101')
# Perform bitwise operations
other_bitarray = bitarray.bitarray([True, False, False, True])
result = my_bitarray & other_bitarray # Bitwise AND operation
print(result) # Output: bitarray('1000')
# Convert bitarray to a regular list of booleans
bit_list = my_bitarray.tolist()
print(bit_list) # Output: [True, False, True, True, False, False, False, False, False, False]
Associative Arrays in Python
In Python, associative arrays are typically implemented using dictionaries. Dictionaries are unordered collections of key-value pairs, where each key is unique and associated with a value. They provide a way to store and retrieve data based on a specific key rather than numerical indices. Here's an example of how to work with dictionaries as associative arrays in Python:
# Creating an associative array (dictionary)
student_scores = {
"John": 85,
"Alice": 92,
"Bob": 78,
"Emily": 95
}
# Accessing values using keys
print(student_scores["John"]) # Output: 85
print(student_scores["Alice"]) # Output: 92
# Modifying values
student_scores["Bob"] = 80
print(student_scores["Bob"]) # Output: 80
# Adding new key-value pairs
student_scores["David"] = 88
# Checking if a key exists
if "Emily" in student_scores:
print("Emily's score:", student_scores["Emily"]) # Output: Emily's score: 95
# Removing a key-value pair
del student_scores["Alice"]
# Iterating over keys and values
for student, score in student_scores.items():
print(student, "scored", score)
Dynamic Array in Python
In Python, you can use the built-in list
data structure to create a dynamic array. A dynamic array in Python is a resizable array that can grow or shrink in size as needed. The list
data structure provides dynamic resizing automatically, allowing you to add or remove elements dynamically without explicitly managing the array's size.
Here's an example of how to create and use a dynamic array in Python:
# Create an empty dynamic array
my_array = []
# Append elements dynamically
my_array.append(1)
my_array.append(2)
my_array.append(3)
print(my_array) # Output: [1, 2, 3]
# Modify elements
my_array[1] = 4
print(my_array) # Output: [1, 4, 3]
# Remove elements
my_array.remove(1)
print(my_array) # Output: [4, 3]
Dive deep into the topic
Contribute with us!
Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.