Python List VS Array VS Tuple
Last Updated :
19 Feb, 2025
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:
Python
a = ["apple", 42, 3.14, True]
# Accessing elements
print(a[0])
# Modifying an element
a[1] = 100
print(a)
Outputapple
['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):
Python
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)
Output1
array('i', [1, 10, 3, 4])
Example(Using Numpy array):
Python
import numpy as np
# Creating a NumPy array
np_array = np.array([1, 2, 3, 4])
print(np_array)
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.
Python
# Creating a tuple
tup = ("apple", 42, 3.14)
# Accessing elements
print(tup[1])
# Print Tuple
print(tup)
Output42
('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 |
---|
Similar Reads
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
Python Lists VS Numpy Arrays Here, we will understand the difference between Python List and Python Numpy array. What is a Numpy array?NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operati
7 min read
Tuples in Python Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.Python# Note : In case of list, we use squa
7 min read
Are Tuples Immutable in Python? Yes, tuples are immutable in Python. This means that once a tuple is created its elements cannot be changed, added or removed. The immutability of tuples makes them a fixed, unchangeable collection of items. This property distinguishes tuples from lists, which are mutable and allow for modifications
2 min read
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
Python | Linear search on list or tuples Let us see a basic linear search operation on Python lists and tuples. A simple approach is to do a linear search, that is Start from the leftmost element of the list and one by one compare x with each element of the list.If x matches with an element, return True.If x doesnât match with any of the e
2 min read