目次
- 1 1. Introduction
- 2 2. What are Python arrays? (List, Tuple, NumPy)
- 3 3. Understanding Python Lists (List)
- 4 4. Understanding Python Tuples
- 5 5. Comparison Table: List vs Tuple – Which Should You Use?
- 6 6. Advanced Array Operations
- 7 7. Frequently Asked Questions (FAQ)
- 7.1 Q1: What is the difference between lists and tuples?
- 7.2 Q2: What is the correct way to copy a list?
- 7.3 Q3: What is a list comprehension?
- 7.4 Q4: How do you convert a list to JSON in Python?
- 7.5 Q5: How do you randomly shuffle the elements of a list?
- 7.6 Q6: What are the differences between NumPy arrays and lists?
- 7.7 Summary
- 8 8. Summary
1. Introduction
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
Ad
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.import numpy as np
# Create list
my_list = [1, 2, 3, 4, 5]
# Create tuple
my_tuple = (1, 2, 3, 4, 5)
# Create NumPy array
my_array = np.array([1, 2, 3, 4, 5])
print(my_list) # [1, 2, 3, 4, 5]
print(my_tuple) # (1, 2, 3, 4, 5)
print(my_array) # [1 2 3 4 5]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]) # bananaList 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, useremove() 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']List Concatenation and Copying
List Concatenation
You can concatenate lists using the+ operator.list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # [1, 2, 3, 4, 5, 6]Copying Lists
When copying a list, using the slice[:] is safe.original_list = [1, 2, 3]
copied_list = original_list[:]
print(copied_list) # [1, 2, 3]Summary
- 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.)
Ad
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.Tuple Basics
How to Create a Tuple
Tuples are created by using parentheses().# Create a tuple
fruits = ("apple", "banana", "cherry")
numbers = (1, 2, 3, 4, 5)
print(fruits) # ('apple', 'banana', 'cherry')
print(numbers) # (1, 2, 3, 4, 5)How to Create a Single-Element Tuple
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]) # greenTuple 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 usingtuple() or list().# List → Tuple
numbers_list = [1, 2, 3]
numbers_tuple = tuple(numbers_list)
print(numbers_tuple) # (1, 2, 3)
# Tuple → List
fruits_tuple = ("apple", "banana", "cherry")
fruits_list = list(fruits_tuple)
print(fruits_list) # ['apple', 'banana', 'cherry']Summary
- 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)✅ Cases Where You Should Use Tuples
- Data that should not be changed
weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")- When used as a dictionary key
location_data = {
(35.6895, 139.6917): "Tokyo",
(40.7128, -74.0060): "New York"
}
print(location_data[(35.6895, 139.6917)]) # TokyoSummary
- 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.
Ad
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]Handling Multidimensional Lists
# Create a 3x3 matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0]) # [1, 2, 3]
print(matrix[1][2]) # 6Converting Between Lists and Dictionaries
# Convert two lists into a dictionary
keys = ["name", "age", "city"]
values = ["Alice", 25, "Tokyo"]
person_dict = dict(zip(keys, values))
print(person_dict) # {'name': 'Alice', 'age': 25, 'city': 'Tokyo'}Sorting Lists and Random Operations
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers) # e.g., [3, 1, 5, 2, 4]
random_choice = random.choice(numbers)
print(random_choice) # e.g., 2Summary
- 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.
Ad
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 assignmentQ2: What is the correct way to copy a list?
A: Usingcopy() 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)]Q4: How do you convert a list to JSON in Python?
import json
data = [1, 2, 3, 4, 5]
json_data = json.dumps(data)
print(json_data) # "[1, 2, 3, 4, 5]"Q5: How do you randomly shuffle the elements of a list?
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print(numbers) # e.g.: [3, 1, 5, 2, 4]Q6: What are the differences between NumPy arrays and lists?
| Item | List | NumPy array (ndarray) |
|---|---|---|
| Data type | Any | Only same-type data |
| Processing speed | Slow | Fast (C-based optimization) |
| Memory usage | High | Low |
| Mathematical operations | for loop required | Vectorized operations possible (fast) |
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr * 2) # [2 4 6 8 10]Summary
In this FAQ section, we explained common questions about Python lists and tuples and their solutions.- Difference between lists and tuples → Different mutability
- Copying a list → Use
copy()or slicing[:] - List comprehensions → More concise than
forloops - List JSON conversion → Leverage
json.dumps()/json.loads() - Differences between NumPy arrays and lists → NumPy is fast and suited for numeric computation
Ad
8. Summary
In this article, we explained in detail the basics to advanced topics of arrays (lists, tuples, NumPy arrays) in Python.What you learned in this article
✅ What are Python arrays?
- Python does not have an “array type” like C
- Instead, “list”, “tuple”, and “NumPy array (ndarray)” are used
✅ Python lists
- Variable-length (mutable) data structure
- Elements can be added, removed, or modified
✅ Python tuples
- Immutable (unchangeable) data structure
- Can be used as dictionary keys
- Faster execution and lower memory usage
✅ List vs Tuple comparison
| Item | List | Tuple |
|---|---|---|
| Mutability | ✅ Mutable | ❌ Immutable |
| Speed | Slower | Fast |
| Memory usage | Higher | Lower |
| Use case | Mutable data management | Immutable data management |




