Lists in Python are the most flexible and commonly used data structure for sequential storage. They are similar to arrays in other languages but with several key differences:
- Dynamic Typing: Python lists can hold elements of different types in the same list. We can have an integer, a string and even other lists all stored within a single list.
- Dynamic Resizing: Lists are dynamically resized, meaning you can add or remove elements without declaring the size of the list upfront.
- Built-in Methods: Python lists come with numerous built-in methods that allow for easy manipulation of the elements within them, including methods for appending, removing, sorting and reversing elements.
a = [1, "Hello", [3.14, "world"]]
a.append(2) # Add an integer to the end
print(a)
Output
[1, 'Hello', [3.14, 'world'], 2]
Note: Python does not have built-in array support in the same way that languages like C and Java do, but it provides something similar through the array module for storing elements of a single type.
NumPy Arrays
NumPy arrays are a part of the NumPy library, which is a powerful tool for numerical computing in Python. These arrays are designed for high-performance operations on large volumes of data and support multi-dimensional arrays and matrices. This makes them ideal for complex mathematical computations and large-scale data processing.
Features:
- Multi-dimensional support: NumPy arrays can handle more than one dimension, making them suitable for matrix operations and more complex mathematical constructs.
- Broad broadcasting capabilities: They can perform operations between arrays of different sizes and shapes, a feature known as broadcasting.
- Efficient storage and processing: NumPy arrays are stored more efficiently than Python lists and provide optimized performance for numerical operations.
import numpy as np
a = np.array([1, 2, 3, 4])
# Element-wise operations
print(a * 2)
# Multi-dimensional array
res = np.array([[1, 2], [3, 4]])
print(res * 2)
Output
[2 4 6 8] [[2 4] [6 8]]
Note: Choose NumPy arrays for scientific computing, where you need to handle complex operations or work with multi-dimensional data.
Use Python's array module when you need a basic, memory-efficient container for large quantities of uniform data types, especially when your operations are simple and do not require the capabilities of NumPy.
Python Arrays
In Python, array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Unlike Python lists (can store elements of mixed types), arrays must have all elements of same type. Having only homogeneous elements makes it memory-efficient.
import array as arr
a = arr.array('i', [1, 2, 3])
# accessing First Araay
print(a[0])
# adding element to array
a.append(5)
print(a)
Output
1
array('i', [1, 2, 3, 5])
Create an Array in Python
Array in Python can be created by importing an array module. array( data_type , value_list ) is used to create array in Python with data type and value list specified in its arguments.
import array as arr
a = arr.array('i', [1, 2, 3])
for i in range(0, 3):
print(a[i], end=" ")
Output
1 2 3