Python Tuples
A tuple is a built-in Python data structure used to store multiple values inside a single variable.
Python provides four main collection data types:
List
Tuple
Set
Dictionary
Each collection type has unique behavior and use cases.
What Is a Tuple?
A tuple is:
Ordered – items maintain their position
Immutable – values cannot be changed after creation
Allows duplicates
Tuples are created using round brackets ().
Creating a Tuple
Example
Tuple Characteristics
Ordered
Items inside a tuple have a fixed order that never changes.
Immutable
Once a tuple is created, its elements cannot be modified.
Allows Duplicate Values
Tuples can store repeated elements.
Tuple Length
To find how many elements a tuple contains, use the len() function.
Creating a Tuple with One Item
A single-item tuple must include a trailing comma, otherwise Python treats it as a string.
Without Comma (Not a Tuple)
Tuple Items and Data Types
Tuple elements can be of any data type.
Examples
Mixed Data Types in a Tuple
Checking Tuple Data Type
From Python’s perspective, tuples are objects of type tuple.
Using the tuple() Constructor
You can also create a tuple using the tuple() function.
Note: Notice the use of double brackets when converting another collection into a tuple.
Accessing Tuple Elements in Python
You can retrieve values from a tuple by referring to their index position using square brackets [].
Access by Index
Each element in a tuple has an index number.
Indexing starts from 0, meaning the first item is at position 0.
Example: Access an Item by Index
Negative Indexing
Python allows negative indexing, which means counting from the end of the tuple.
-1 → last element
-2 → second last element
Example: Access the Last Item
Accessing a Range of Items
You can extract multiple items by specifying a start index and an end index.
Note: The start index is included, but the end index is excluded.
Example: Extract a Subset of a Tuple
Slice from the Beginning
If you omit the starting index, slicing begins from the first element.
Example
Slice Until the End
If you omit the ending index, slicing continues to the last element.
Example
Using Negative Index Ranges
Negative indexes can also be used when slicing a tuple.
Example: Slice Using Negative Indexes
Checking if an Item Exists
To check whether a value exists inside a tuple, use the in keyword.
Example: Membership Test
Updating Tuples in Python
Tuples in Python are immutable, which means once they are created, you cannot directly modify, add, or remove their elements.
However, Python provides safe workarounds to achieve these operations.
Modifying Tuple Elements (Workaround)
Since tuples cannot be edited directly, the common solution is:
Convert the tuple into a list
Make the required change
Convert it back into a tuple
Example: Change an Existing Value
Adding New Items to a Tuple
Method 1: Convert Tuple to List
This is the most flexible approach for adding one or more items.
Example: Append a New Element
Method 2: Join Two Tuples
You can also add elements by creating a new tuple and merging it with the existing one.
Note: Remember to include a comma when creating a single-item tuple.
Example: Extend a Tuple Using Concatenation
Removing Items from a Tuple
Direct removal is not allowed because tuples are immutable.
Again, the solution is to convert the tuple into a list.
Example: Remove an Item
Deleting a Tuple Entirely
If you no longer need a tuple, you can delete it completely using the del keyword.
Example: Delete the Tuple Object
Unpacking Tuples in Python
When you assign multiple values to a tuple, the process is called tuple packing.
Python also allows you to extract those values into separate variables, which is known as tuple unpacking.
Packing a Tuple
Packing happens when multiple values are grouped into a tuple.
Example: Creating a Packed Tuple
Basic Tuple Unpacking
You can assign each element of a tuple to individual variables in a single line.
Note:The number of variables must match the number of tuple items.
Example: Unpack Values into Variables
Using the Asterisk (*) Operator
When the tuple contains more values than variables, use the * operator to collect extra values into a list.
Collect Remaining Values into a List
Example: Unpacking with an Asterisk at the End
Asterisk in the Middle
You can also place the * operator in the middle of variable names.
Python will assign values until the remaining variables can be filled.
Example: Capture Middle Values
Looping Through Tuples in Python
Tuples are iterable objects, which means you can loop through their elements using different looping techniques in Python.
Iterating Directly Over Tuple Values
The simplest way to loop through a tuple is by using a for loop.
Example: Loop Through Tuple Elements
Looping Using Index Positions
Sometimes, you may need access to the index of each item.
In such cases, use range() together with len().
Example: Access Items Using Index
Iterating with a While Loop
A while loop can also be used when you want full control over the index value.
Example: Loop Using While
Choosing the Right Loop
Loop Type |
Best Use Case |
for loop |
Simple iteration over values |
for with index |
When index is required |
while loop |
Manual control over iteration |
Joining Tuples in Python
Python allows you to combine and repeat tuples using simple operators. These operations always create new tuples, because tuples are immutable.
Combining Multiple Tuples
You can merge two or more tuples using the + operator.
The result is a new tuple containing elements from all tuples in order.
Example: Join Two Tuples
Joining More Than Two Tuples
You can also chain multiple tuples together.
Example
Repeating Tuple Elements
The * operator allows you to repeat the elements of a tuple a specified number of times.
Example: Duplicate Tuple Values
Practical Example
Example: Generate Repeated Data
Tuple Methods in Python
Tuples in Python are immutable, which means they have very few built-in methods.
Unlike lists, tuples support only two main methods for working with their data.
Method |
Purpose |
count() |
Counts occurrences of a value |
index() |
Returns the index of the first match |
count() Method
The count() method returns how many times a specific value appears in a tuple.
Example: Count Occurrences of a Value
index() Method
The index() method finds the first position of a specified value in a tuple.
Note: If the value does not exist, Python raises an error.
Example: Find Index of an Element
Handling Errors Safely
Before using index(), it’s a good practice to check whether the value exists.