In Python, List, Array and Tuple are data structures for storing multiple elements. Lists are dynamic and hold mixed types, Arrays are optimized for numerical data with the same type and Tuples are immutable, ideal for fixed collections. Choosing the right one depends on performance and data needs.
List in Python
A List is an ordered, mutable collection of elements. It is the most flexible data structure in Python and is widely used due to its versatility.
Key characteristics:
- Mutable: Elements can be changed after the list is created.
- Ordered: Elements maintain their order.
- Heterogeneous: Can hold elements of different data types.
- Dynamic: The size of the list can grow or shrink dynamically.
- Indexing: Elements can be accessed using indices (starting from 0).
Example:
a = ["apple", 42, 3.14, True]
# Accessing elements
print(a[0])
# Modifying an element
a[1] = 100
print(a)
Output
apple ['apple', 100, 3.14, True]
Array in Python
An Array is a collection of elements of the same data type. Unlike lists, arrays are more efficient when performing numerical computations or storing large amounts of uniform data.
Types of arrays:
- Array Module: Provided by the built-in array module.
- NumPy Arrays: Provided by the third-party library NumPy. More efficient and feature-rich compared to the built-in array module.
Key Characteristics:
- Mutable: Elements can be modified after creation.
- Ordered: Maintains the order of elements.
- Homogeneous: Stores elements of the same data type.
- Efficient: Provides better performance for numerical operations compared to lists.
Example( Using array Module):
import array as arr
# Creating an integer array
a = arr.array('i', [1, 2, 3, 4])
# Accessing elements
print(a[0])
# Modifying an element
a[1] = 10
print(a)
Output
1
array('i', [1, 10, 3, 4])
Example(Using Numpy array):
import numpy as np
# Creating a NumPy array
np_array = np.array([1, 2, 3, 4])
print(np_array)
Output
[1 2 3 4]
Tuple in Python
A Tuple is an ordered and immutable collection of elements. It is often used when we want to ensure that a sequence of values remains constant throughout the program.
Key characteristics:
- Immutable: Once created, elements cannot be modified.
- Ordered: Maintains the order of elements.
- Heterogeneous: Can store different data types.
- Faster: Accessing elements in a tuple is faster compared to lists due to its immutable nature.
# Creating a tuple
tup = ("apple", 42, 3.14)
# Accessing elements
print(tup[1])
# Print Tuple
print(tup)
Output
42
('apple', 42, 3.14)
Key Difference Between List, Array and Tuple
Feature | List | Array | Tuple |
|---|---|---|---|
Mutability | Mutable | Mutable | Immutable |
Data Type | Can store different types | Stores elements of the same type | Can store different types |
Ordered | Yes | Yes | Yes |
Performance | Slower for numerical operations | Faster for numerical operations | Faster than lists (due to immutability) |
Memory Efficiency | Less efficient | More efficient for large data | More memory-efficient than lists |
Usage | General-purpose collection | Numerical and homogeneous data | Fixed data, constants |
Syntax | [ ] | array() | ( ) |
Indexing | Supported | Supported | Supported |