Python
Programming
Data Structures
Tuple
List Conversion

Convert tuple to list and back

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, converting between tuples and lists is done with the built-in list() and tuple() constructors. list(my_tuple) creates a mutable list from a tuple, and tuple(my_list) creates an immutable tuple from a list. This is a common operation when you need to modify a sequence that was returned as a tuple, or when you need to use a sequence as a dictionary key (which requires immutability).

Tuple to List

python
1my_tuple = (1, 2, 3, 4, 5)
2my_list = list(my_tuple)
3
4print(my_list)        # [1, 2, 3, 4, 5]
5print(type(my_list))  # <class 'list'>
6
7# Now you can modify it
8my_list.append(6)
9my_list[0] = 10
10print(my_list)  # [10, 2, 3, 4, 5, 6]

List to Tuple

python
1my_list = [1, 2, 3, 4, 5]
2my_tuple = tuple(my_list)
3
4print(my_tuple)        # (1, 2, 3, 4, 5)
5print(type(my_tuple))  # <class 'tuple'>
6
7# Tuples are immutable — cannot modify
8# my_tuple[0] = 10  # TypeError: 'tuple' object does not support item assignment

Common Use Case: Modify a Tuple

Since tuples are immutable, the only way to "modify" one is to convert to a list, make changes, and convert back:

python
1# Original tuple
2coordinates = (10, 20, 30)
3
4# "Modify" by converting to list and back
5temp = list(coordinates)
6temp[1] = 25
7coordinates = tuple(temp)
8
9print(coordinates)  # (10, 25, 30)

Nested Tuples and Lists

list() and tuple() only convert the outermost level. Nested structures keep their original types:

python
1nested_tuple = ((1, 2), (3, 4), (5, 6))
2converted = list(nested_tuple)
3print(converted)        # [(1, 2), (3, 4), (5, 6)]
4print(type(converted[0]))  # <class 'tuple'> — inner tuples unchanged
5
6# Deep conversion requires recursion or comprehension
7deep_converted = [list(inner) for inner in nested_tuple]
8print(deep_converted)  # [[1, 2], [3, 4], [5, 6]]
python
1# Deep conversion in both directions
2def deep_to_list(obj):
3    if isinstance(obj, (tuple, list)):
4        return [deep_to_list(item) for item in obj]
5    return obj
6
7def deep_to_tuple(obj):
8    if isinstance(obj, (tuple, list)):
9        return tuple(deep_to_tuple(item) for item in obj)
10    return obj
11
12nested = ((1, [2, 3]), (4, (5, 6)))
13print(deep_to_list(nested))   # [[1, [2, 3]], [4, [5, 6]]]
14print(deep_to_tuple(nested))  # ((1, (2, 3)), (4, (5, 6)))

Converting for Dictionary Keys

Tuples can be dictionary keys because they are hashable (if their contents are hashable). Lists cannot:

python
1# Tuple as dictionary key — works
2point_data = {(0, 0): "origin", (1, 0): "right", (0, 1): "up"}
3print(point_data[(0, 0)])  # "origin"
4
5# List as dictionary key — fails
6# {[0, 0]: "origin"}  # TypeError: unhashable type: 'list'
7
8# Convert list to tuple for use as key
9coords = [0, 0]
10point_data[tuple(coords)] = "origin"

Converting for Function Arguments

Use * to unpack a list or tuple as function arguments:

python
1def add(a, b, c):
2    return a + b + c
3
4values_tuple = (1, 2, 3)
5values_list = [1, 2, 3]
6
7print(add(*values_tuple))  # 6
8print(add(*values_list))   # 6

Performance Comparison

python
1import timeit
2
3data = list(range(1000))
4
5# List to tuple
6timeit.timeit(lambda: tuple(data), number=100_000)  # ~0.5s
7
8# Tuple to list
9data_tuple = tuple(data)
10timeit.timeit(lambda: list(data_tuple), number=100_000)  # ~0.5s
11
12# Both are O(n) — every element is copied

Tuples use slightly less memory than lists because they are fixed-size:

python
1import sys
2
3data = list(range(100))
4print(sys.getsizeof(data))          # ~920 bytes (list)
5print(sys.getsizeof(tuple(data)))   # ~848 bytes (tuple)

Working with Named Tuples

python
1from collections import namedtuple
2
3Point = namedtuple('Point', ['x', 'y'])
4p = Point(10, 20)
5
6# Convert to list
7p_list = list(p)
8print(p_list)  # [10, 20]
9
10# Convert to dict (named tuples support this)
11p_dict = p._asdict()
12print(p_dict)  # {'x': 10, 'y': 20}
13
14# Modify and create new named tuple
15p_list[0] = 15
16p2 = Point(*p_list)
17print(p2)  # Point(x=15, y=20)
18
19# Or use _replace for immutable updates
20p3 = p._replace(x=15)
21print(p3)  # Point(x=15, y=20)

Common Pitfalls

  • Assuming conversion is deep: list(((1,2), (3,4))) gives [(1,2), (3,4)] — inner tuples remain tuples. For full conversion of nested structures, use a recursive function or list comprehension.
  • Modifying the converted list affects nothing: list(my_tuple) creates a new list. Changing the list does not affect the original tuple (and vice versa). They are independent copies (shallow copy).
  • Using lists as dictionary keys or set elements: Lists are unhashable and cannot be used as dictionary keys or in sets. Convert to tuples first: my_set.add(tuple(my_list)).
  • Forgetting single-element tuple syntax: (1) is not a tuple — it is the integer 1 in parentheses. A single-element tuple requires a trailing comma: (1,). list((1,)) gives [1], but list((1)) raises TypeError.
  • Performance with large sequences: Both list() and tuple() copy every element, which is O(n). For very large sequences, avoid unnecessary back-and-forth conversion. If you need mutability, keep the data as a list throughout.

Summary

  • list(my_tuple) converts a tuple to a mutable list; tuple(my_list) converts back
  • Conversion is shallow — nested structures retain their original types
  • Use list conversion when you need to modify an immutable tuple
  • Use tuple conversion when you need a hashable sequence (dictionary keys, set elements)
  • Both operations are O(n) and create independent copies (shallow)
  • Single-element tuples require a trailing comma: (value,)

Course illustration
Course illustration

All Rights Reserved.