Exploring Python Tuples: Immutable Data Structures

| 3 min read

What is a Python Tuple?

A Tuple Python is an ordered collection of elements, much like a list. However, what sets tuples apart is their immutability. Once created, the elements within a tuple cannot be modified, added or removed. This immutability lends tuples a sense of stability and reliability, making them useful in scenarios where data integrity is paramount.

How Python Tuples Works?

To create a tuple in Python, one simply enclose the elements within parentheses, separated by a commas.

my_tuple = (1, 2, 3, "a", "b", "c")

# Access to an item in tuple
print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: 'a'

Tuple Methods:

  • count()
my_tuple = (1, 2, 2, 3, 4, 2)

print(my_tuple.count(2))
# Output: 3 (as '2' appears three times in the tuple)
  • index()
my_tuple = ("a", "b", "c", "a", "d")

print(my_tuple.index("a"))
# Output: 0 (index of first occurrence of 'a')
print(my_tuple.index("b"))
# Output: 1 (index of first occurrence of "b")

Operations on Tuples:

  • Join Tuples:
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)
# Output: ('a', 'b', 'c', 1, 2, 3)
  • Unpack Tuples:
num_tuple = (1, 2, 3)
(one, two, three) = num_tuple

print(one) # Output: 1
print(two) # Output: 2
print(three) # Output: 3

Differences between Tuples and Lists

Syntax:

  • Tuples: Defined using parentheses and elements separated by commas.
    • Example: (1, 2, 3)
  • Lists: Defined using square brackets and elements separated by commas.
    • Example: [1, 2, 3]

Mutability:

  • Tuples: Immutable; elements cannot be altered after creation.
  • Lists: Mutable; elements can be altered after creation.

Performance:

  • Tuples: Offer slightly better performance because of immutability, especially in scenarios involving iteration and accessing elements.
  • Lists: May have slightly lower performance compared to tuples because of mutability, especially in scenarios involving iteration and accessing elements.

Use Cases:

  • Tuples: Preferred for situations where data integrity and immutability are crucial, such as representing fixed collections of elements (e.g., coordinates, database records).
  • Lists: Favored for dynamic data manipulation, such as adding, removing, or modifying elements, as they support such operations efficiently.

Speed Comparison between List and Tuple:

from timeit import timeit

num = 1000000
list_time = timeit(stmt=lambda: ["red", "blue", 1, 2, 3], number=num)
tuple_time = timeit(stmt=lambda: ("red", "blue", 1, 2, 3), number=num)

print("List creation time:")
print(list_time)
print("Tuple creation time:")
print(tuple_time)

# Output:
# List creation time:
# 👉 0.14130980100003399
# Tuple creation time:
# 👉 0.07253469400006907

This code will output the time taken to create lists and tuples, each repeated 1,000,000 times. By comparing these times, you can observe the difference in speed between creating lists and tuples.

Thank you!

Thank you for your time and for reading this!