Convert list to tuple in Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Converting a List to a Tuple in Python
Python, a language celebrated for its simplicity and readability, provides two fundamental data structures: lists and tuples. While both are used to store collections of data, they have distinct properties and use cases. This article delves into the process of converting a list to a tuple in Python, covering technical explanations, examples, and additional details that newcomers and seasoned developers will find valuable.
Lists vs. Tuples
Before we discuss conversion, it’s essential to understand the differences between lists and tuples:
- Lists:
- Mutable, meaning items can be added, removed, or changed.
- Defined with square brackets:
[]. - Suitable for collections that need to be modified.
- Tuples:
- Immutable, meaning once defined, they cannot be altered.
- Defined with parentheses:
(). - Suitable for collections that should remain constant after verification.
| Property | List | Tuple |
| Mutability | Mutable | Immutable |
| Syntax | list_var = [1, 2, 3] | |
tuple_var = (1, 2, 3) | ||
| Use Case | Dynamic collection | Static collection |
| Performance | Slower due to mutability | Faster due to immutability |
Conversion from List to Tuple
In Python, converting data structures is often straightforward due to its built-in functions and methods. To convert a list to a tuple, you can simply use the tuple()
function. Here is a step-by-step explanation and examples:
Using tuple()
Function
The tuple()
function is a built-in Python function that converts the specified iterable (like a list) into a tuple. Here’s a simple example:
- We define a list
my_list. - We use
tuple(my_list)to convert it into a tuple and assign it tomy_tuple. - When printed,
my_tuplereflects the immutable tuple version of the original list. - Constant Data: If your list represents fixed data, converting it to a tuple ensures that it remains unchanged.
- Performance Optimization: In performance-intensive applications, using tuples can offer speed benefits.
- Dictionary Keys: If there is a need to use list-like structures as dictionary keys, conversion to a tuple is necessary.
- Each inner list is converted to a tuple via a generator expression inside the
tuple()function. - The entire structure now becomes a tuple of tuples.
Related reading
- Convert ListT to ObservableCollectionT in WP7
- Convert NumPy array to Python list
- Convert parent-child array to tree
- Convert Set to List without creating new List
- Convert Pandas column containing NaNs to dtype int
- Convert Pandas Column to DateTime
- Convert Sorted Array to Binary Search Tree
- Convert String array to ArrayList

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.