Skip to main content

Basic Operations with Tuples

Basic Operations with Tuples in Python

Even though tuples are immutable, they support various operations such as indexing, slicing, and concatenation that can be performed on them. In this article, we will explore the different operations that can be performed on tuples in Python and their syntax.

Sorting List of Tuples in Python

Tuples can be sorted using the built-in sorted() function or the sort() method. Here's an example of sorting tuples based on the first element:

tuples = [(3, 'apple'), (1, 'orange'), (2, 'banana')]
sorted_tuples = sorted(tuples, key=lambda x: x[0])
print(sorted_tuples)  # Output: [(1, 'orange'), (2, 'banana'), (3, 'apple')]

Alternatively, you can use the sort() method on the list of tuples, like this:

tuples = [(3, 'apple'), (1, 'orange'), (2, 'banana')]
tuples.sort(key=lambda x: x[0])
print(tuples)  # Output: [(1, 'orange'), (2, 'banana'), (3, 'apple')]

The sort() method works similarly to sorted(), but sorts the list in place rather than returning a new sorted list.

Tuples Comparing

To compare tuples in Python, you can compare their elements one by one in a pairwise fashion until a difference is found, or until all elements have been compared successfully.

Here's an example code that compares two tuples:

tuple1 = (1, 2, 3)
tuple2 = (1, 2, 4)

if tuple1 == tuple2:
    print("The two tuples are equal")
    print("The two tuples are not equal")

Tuples Indexing in Python

Tuples can be indexed using square brackets [] and an index number. The index number starts from 0 for the first element and increases by 1 for each subsequent element. For example, consider the following tuple:

my_tuple = (10, 20, 30, 40, 50)

To access the first element of the tuple, you would use the index 0 like this:

print(my_tuple[0])   # Output: 10

To access the third element of the tuple, you would use the index 2 like this:

print(my_tuple[2])   # Output: 30

Tuple Slicing

A tuple is an ordered, immutable collection of elements, and you can use slicing to extract a portion of the tuple.

Tuple slicing uses the syntax tuple[start:end:step] to specify a range of elements to be extracted from the tuple. The start parameter is the index of the first element to include in the slice (inclusive), end is the index of the last element to include in the slice (exclusive), and step is the increment between the indices.

Here's an example:

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4])  # Output: (2, 3, 4)

You can also use negative indexing to slice a tuple from the end. For example:

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[-3:-1])  # Output: (3, 4)

If you don't specify any of the parameters, Python uses the default values: start=0, end=len(tuple), and step=1. For example:

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[:3])  # Output: (1, 2, 3)

Returning a Tuple

You can use a tuple to return multiple values from a function.

Here's an example of a function that returns a tuple:

def calculate_statistics(numbers):
    total = sum(numbers)
    count = len(numbers)
    average = total / count
    return total, count, average

Tuple Comprehension in Python

Tuple comprehension is a feature in Python that allows you to create a new tuple from an existing iterable by applying a transformation to each element of the iterable.

The syntax for tuple comprehension is similar to list comprehension, except that the result is enclosed in parentheses instead of square brackets. Here's an example:

numbers = (1, 2, 3, 4, 5)
squares = tuple(x ** 2 for x in numbers)
print(squares)  # Output: (1, 4, 9, 16, 25)

Tuple comprehension can also include conditional expressions, which allows you to filter elements based on some condition. Here's an example:

numbers = (1, 2, 3, 4, 5)
even_squares = tuple(x ** 2 for x in numbers if x % 2 == 0)
print(even_squares)  # Output: (4, 16)

Length of Tuple in Python

You can find the length of a tuple using the built-in len() function.

For example, suppose you have a tuple named my_tuple:

my_tuple = (1, 2, 3, 4, 5)
print(len(my_tuple)) # Output: 5

Looping Through a Tuple

To loop through a tuple in Python, you can use a for loop. Here's an example:

my_tuple = (1, 2, 3, 4, 5)
for item in my_tuple:

If you also need the index of each item in the tuple, you can use the enumerate() function:

my_tuple = (1, 2, 3, 4, 5)
for index, item in enumerate(my_tuple):
    print(index, item)

Contribute with us!

Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.

Profile picture for user almargit
Python developer, content manager.
Profile picture for user AliaksandrSumich
Updated: 05/03/2024 - 21:53
Profile picture for user angarsky
Reviewed and approved