Check with all() and zip()
In this step, you will learn how to use the all()
and zip()
functions in conjunction with sorted tuples to perform more advanced checks and comparisons.
The all()
function is a built-in Python function that returns True
if all elements of an iterable are true (or if the iterable is empty). It's often used to check if a condition is met for all elements in a sequence.
The zip()
function is another built-in Python function that takes multiple iterables as arguments and returns an iterator of tuples. Each tuple contains the corresponding elements from the input iterables. It's useful for pairing elements from different sequences for comparison or other operations.
Let's see how these functions can be used with sorted tuples:
- Open the VS Code editor in the LabEx environment.
- Modify the existing file
sort_tuple.py
in the ~/project
directory, or create it if it doesn't exist.
- Copy and paste the following code into
sort_tuple.py
:
## Checking if a tuple is sorted using all() and zip()
def is_sorted(data):
## zip(data, data[1:]) pairs consecutive elements
## all(x <= y for x, y in ...) checks if each pair is in ascending order
return all(x <= y for x, y in zip(data, data[1:]))
my_tuple1 = (1, 2, 3, 4, 5)
my_tuple2 = (5, 2, 8, 1, 9)
print("Tuple 1:", my_tuple1, "is sorted:", is_sorted(my_tuple1))
print("Tuple 2:", my_tuple2, "is sorted:", is_sorted(my_tuple2))
This code defines a function is_sorted()
that checks if a given tuple is sorted in ascending order. It uses zip()
to pair consecutive elements of the tuple and all()
to check if each pair is in ascending order.
To run the script, open a terminal in VS Code (you can find it in the bottom panel) and execute the following command:
python sort_tuple.py
You should see the following output:
Tuple 1: (1, 2, 3, 4, 5) is sorted: True
Tuple 2: (5, 2, 8, 1, 9) is sorted: False
As you can see, the is_sorted()
function correctly identifies whether the tuples are sorted or not.
Let's extend this example to check if two tuples are identical after sorting:
## Checking if two tuples are identical after sorting
def are_identical_after_sorting(tuple1, tuple2):
return sorted(tuple1) == sorted(tuple2)
tuple_a = (3, 1, 4, 1, 5)
tuple_b = (1, 5, 1, 4, 3)
tuple_c = (1, 2, 3, 4, 5)
print("Tuple A:", tuple_a, "and Tuple B:", tuple_b, "are identical after sorting:", are_identical_after_sorting(tuple_a, tuple_b))
print("Tuple A:", tuple_a, "and Tuple C:", tuple_c, "are identical after sorting:", are_identical_after_sorting(tuple_a, tuple_c))
Add this code to your sort_tuple.py
file and run it again:
python sort_tuple.py
The output should now include:
Tuple A: (3, 1, 4, 1, 5) and Tuple B: (1, 5, 1, 4, 3) are identical after sorting: True
Tuple A: (3, 1, 4, 1, 5) and Tuple C: (1, 2, 3, 4, 5) are identical after sorting: False
In this example, the are_identical_after_sorting()
function checks if two tuples contain the same elements, regardless of their original order. It sorts both tuples and then compares the sorted lists.