Python is a programming language with simple and intuitive syntax that is used in many fields. Among its features, “arrays” play an important role in managing and manipulating data. However, Python does not have array types like those in C; instead, list (List), tuple (Tuple), NumPy array (ndarray) and other data structures are used. Because each data structure has different characteristics and uses, it is important to choose appropriately.
What are Python arrays?
List (List): mutable (changeable) and highly flexible data structure
Tuple (Tuple): immutable (unchangeable) and fast in processing speed
NumPy array (ndarray): array type specialized for numerical computation and data analysis
In this article, we will provide a complete guide from the basics to advanced topics of Python arrays. To make it easy for beginners, we will explain with concrete code examples.
2. What are Python arrays? (List, Tuple, NumPy)
In Python, the “array” data type is not provided in the standard library. Therefore, people generally use lists (List) to handle data like arrays. Also, when dealing with immutable data, use tuples (Tuple), and for efficient numeric operations, use NumPy’s ndarray.
Data structures used as Python arrays
Data Structure
Features
When to use
List (List)
Mutable (modifiable) and can handle data flexibly
General array operations
Tuple (Tuple)
Immutable (non-modifiable) and enables fast processing
Management of constant data
NumPy array (ndarray)
Optimized for large-scale numeric computation and data analysis
Scientific computing and machine learning
Basic usage of lists, tuples, and NumPy arrays
In Python, you can create lists, tuples, and NumPy arrays as follows.
Differences between lists, tuples, and NumPy arrays
Item
List (List)
Tuple (Tuple)
NumPy array (ndarray)
Mutability
✅ Mutable
❌ Immutable
✅ Mutable
Speed
Normal
Fast
Very fast
Memory usage
Higher
Lower
Optimized
Use case
General data manipulation
Management of non-modifiable data
Scientific computing & machine learning
3. Understanding Python Lists (List)
Python lists (List) are data structures similar to variable-length (mutable) arrays. Using lists, you can store different data types such as numbers and strings together in a single data structure. Lists are one of the most fundamental and commonly used data structures in Python.
List Basics
How to Create a List
In Python, lists are created using square brackets [].
# Create an empty list
empty_list = []
# List containing numbers
numbers = [1, 2, 3, 4, 5]
# List containing strings
fruits = ["apple", "banana", "cherry"]
# It's also possible to mix different data types
mixed_list = [1, "hello", 3.14, True]
print(numbers) # [1, 2, 3, 4, 5]
print(fruits) # ['apple', 'banana', 'cherry']
print(mixed_list) # [1, 'hello', 3.14, True]
Accessing List Elements
You can access each element of a list using an index (subscript). Python indices start at 0, and you can also use negative indices to access elements from the end.
# Retrieve element by specifying index
print(fruits[0]) # apple
print(fruits[1]) # banana
# Use negative index (access from the end)
print(fruits[-1]) # cherry
print(fruits[-2]) # banana
List Slicing (Partial Retrieval)
Using slicing, you can retrieve a portion of a list.
numbers = [0, 1, 2, 3, 4, 5 6, 7, 8, 9]
# Get the first 5 elements
print(numbers[:5]) # [0, 1, 2, 3, 4]
# Get elements from the 3rd to the 7th
print(numbers[2:7]) # [2, 3, 4, 5, 6]
# Get the last 3 elements
print(numbers[-3:]) # [7, 8, 9]
# Get elements with a step of 2
print(numbers[::2]) # [0, 2, 4, 6, 8]
Modifying List Elements
Lists are mutable (changeable), so you can modify elements.
# Change the second element
fruits[1] = "orange"
print(fruits) # ['apple', 'orange', 'cherry']
Adding and Removing List Elements</, you can add elements using append() or insert().
# Append element to the end of the list
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
# Insert element at a specified position
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana', 'cherry']
Removing Elements
To remove elements from a list, use remove() or pop().
# Remove element with the specified value
fruits.remove("banana")
print(fruits) # ['apple', 'cherry']
# Remove element at the specified index
fruits.pop(1)
print(fruits) # ['apple']
Lists are variable-length data structures, allowing addition, removal, and modification of elements
Access elements using indices and slices
Learned how to efficiently perform list operations (addition, removal, concatenation, sorting, etc.)
4. Understanding Python Tuples
Python’s Tuple is a data structure that can store multiple values like a list, but it has the characteristic of being immutable.
In other words, the elements of a tuple that has been created cannot be changed, added, or removed. Because of this property, tuples are used in situations where data immutability must be guaranteed. They also have the advantage of being more memory-efficient and faster than lists.
When creating a tuple, you need to be careful if there is only one element. If you don’t include a comma ,, it will be treated as a plain number or string.
single_value = (42) # Not a tuple, an integer
correct_tuple = (42,) # This is a tuple
print(type(single_value)) # <class 'int'>
print(type(correct_tuple)) # <class 'tuple'>
Accessing Tuple Elements
Each element of a tuple can be accessed using an index (subscript). Like lists, Python indices start at 0.
colors = ("red", "green", "blue")
# Access by specifying an index
print(colors[0]) # red
print(colors[1]) # green
# Using negative indices
print(colors[-1]) # blue
print(colors[-2]) # green
Tuple Slicing (Partial Retrieval)
Tuples can also perform slicing (partial retrieval) just like lists.
numbers = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
# Get the first five elements
print(numbers[:5]) # (0, 1, 2, 3, 4)
Converting Between Tuples and Lists
Tuples and lists can be converted back and forth using tuple() or list().
Tuples are immutable and cannot be changed after creation
More memory-efficient and faster than lists
Used in situations that require data immutability (constants, dictionary keys, function return values, etc.)
Can be converted to/from lists, but use a list when mutability is needed
5. Comparison Table: List vs Tuple – Which Should You Use?
In Python, both list (List) and tuple (Tuple) can be used like arrays. However, they have important differences, and using them appropriately is important.
Differences Between Lists and Tuples
First, we organize the main differences between lists and tuples in a comparison table.
Item
List
Tuple
Mutability
✅ Mutable (add, delete, update)
❌ Immutable (unchangeable)
Speed
Slower
Fast
Memory Usage
Higher
Lower
Adding/Removing Elements
Possible
Impossible
Use as Dictionary Key
❌ Not allowed
✅ Allowed
Use Cases
Managing mutable data
Managing immutable data
Choosing Between Lists and Tuples
✅ Cases Where You Should Use Lists
When you need to modify data
users = ["Alice", "Bob", "Charlie"]
users.append("David") # Add a new user
print(users) # ['Alice', 'Bob', 'Charlie', 'David']
When handling variable-length data
user_input = []
while True:
data = input("Enter data (type 'exit' to finish):")
if data == "exit":
break
user_input.append(data)
print(user_input)
location_data = {
(35.6895, 139.6917): "Tokyo",
(40.7128, -74.0060): "New York"
}
print(location_data[(35.6895, 139.6917)]) # Tokyo
Summary
Lists are mutable data structures that allow adding, removing, and modifying data.
Tuples are immutable and are used for data that doesn’t need to change or as dictionary keys.
When dealing with dynamic data, lists are more suitable.
6. Advanced Array Operations
Python’s lists (List) and tuples (Tuple) enable more advanced data processing when mastered. In this section, we introduce advanced array manipulation techniques to help you apply them in practical programs.
List Comprehensions (Efficient List Creation)
# Regular list creation
squares = []
for x in range(10):
squares.append(x**2)
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# List comprehension
squares = [x**2 for x in range(10)]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
By leveraging list comprehensions, you can create lists efficiently.
You can handle matrix and table data using multidimensional lists.
Converting between lists and dictionaries allows flexible data management.
Utilizing list sorting and random operations enables advanced data processing.
7. Frequently Asked Questions (FAQ)
Python’s lists and tuples are extremely useful data structures, but you might have many questions about how to use them.
In this section, we will explain common questions about Python lists and tuples in a Q&A format.
Q1: What is the difference between lists and tuples?
A:Lists are mutable (changeable), while tuples are immutable (unchangeable) is the biggest difference.
# List (mutable)
my_list = [1, 2, 3]
my_list[0] = 100 # mutable
print(my_list) # [100, 2, 3]
# Tuple (immutable)
my_tuple = (1, 2, 3)
my_tuple[0] = 100 # TypeError: 'tuple' object does not support item assignment
Q2: What is the correct way to copy a list?
A: Using copy() or the slice [:] is safe.
# Use slicing
list_a = [1, 2, 3]
list_b = list_a[:]
list_b[0] = 100
print(list_a) # [1, 2, 3] ← The original list remains unchanged!
Q3: What is a list comprehension?
# Regular list creation
squares = []
for x in range(10):
squares.append(x**2)
# ✅ Using a list comprehension makes it concise
squares = [x**2 for x in range(10)]
Understanding Python arrays (lists, tuples, NumPy) and using them appropriately makes data management and processing simpler and more efficient. Please write some code and try it out!