Tuple in Puthon
Tuple in Puthon
# Example 2
tuple3 = (1, 2, 'apple')
tuple4 = (1, 2, 'banana')
result = (tuple3 > tuple4) # False, because 'apple' < 'banana'
print(result)
# Example 3
tuple5 = (1, 2, 'apple')
tuple6 = (1, 2, 3)
result = (tuple5 > tuple6) #TypeError: '>' not supported between instances of 'str' and 'int'
print(result)
Comparing Tuple
• DSU stands for Decorate-Sort-Undecorate, and it 2. Sort:
is a programming pattern used to sort a
collection of elements based on some criteria. • Once the elements are decorated, the
• DSU stands for Decorate-Sort-Undecorate, and next step is to sort the collection based
it's a programming pattern often used in Python. on the associated keys.
This pattern involves three steps: decorating a • Python's built-in sorted() function can be
list, sorting it, and then undecorating it. It's a
flexible approach that allows you to customize used for this purpose.
the sorting process. • The sorted() function takes an iterable
• While the term is often associated with lists, the and returns a new sorted list.
same concept can be applied to tuples.
3. Undecorate:
• Here's a breakdown of the DSU pattern:
1. Decorate:
• After sorting, the final step is to
"undecorate" the sorted collection.
• The first step involves "decorating" the elements
of the collection. • This means removing the temporary
• This means associating each element with a key
association of elements with keys.
based on which the sorting will be performed. • The result is a sorted collection of the
• In Python, this is often done using a function that original elements.
takes an element as input and returns a key. The
key can be a single value or a tuple of values.
# Example: Sorting a list of tuples based on the second element of each tuple
# Step 1: Decorate In this example:
def decorate(tuple): • The decorate function takes a tuple and returns
# Decorating with the second element as the key its second element as the key.
return tuple[1] • The sorted() function is used with the key
parameter set to the decorate function,
indicating that the sorting should be based on
# Original list of tuples the second element of each tuple.
my_list = [(1, 5), (2, 3), (3, 7), (4, 1)] • The result is a new list of tuples sorted based on
the second element of each tuple.
# Step 2: Sort
sorted_list = sorted(my_list, key=decorate)
# Step 3: Undecorate
result = sorted_list
print(result)
Tuple Assignment
Tuple assignment in Python is a feature that allows Explanation:
you to assign multiple variables simultaneously
using a single tuple. It's a convenient way to In this example, coordinates is a tuple with
unpack values from a tuple and assign them to two elements (3 and 7).
multiple variables in a single line. Tuple assignment
is commonly used when working with functions
The line x, y = coordinates is a tuple
that return multiple values, or when dealing with assignment, where the values from the
data in tuples. tuple coordinates are unpacked and
Example: assigned to the variables x and y.
# Creating a tuple After the assignment, x will be 3, and y will
coordinates = (3, 7) be 7.
# Tuple assignment
x, y = coordinates
# Printing the values of x and y
print("x =", x)
print("y =", y)
You can also use tuple assignment with functions that return multiple values.
# Function returning multiple values Explanation:
def get_coordinates(): In this example, the function
return 5, 10 get_coordinates returns a tuple (5, 10),
and tuple assignment is used to assign
# Tuple assignment with function return values these values to variables a and b.
a, b = get_coordinates()
t.sort()
print(t)
Multiple assignment with dictionaries
• Combining items, tuple assignment, and for, you can see a nice code pattern for
traversing the keys and values of a dictionary in a single loop:
d = {'a':10, 'b':1, 'c':22}
for key, val in list(d.items()):
print(val, key)
• This loop has two iteration variables because items returns a list of tuples and key, val is
a tuple assignment that successively iterates through each of the key-value pairs in the
dictionary (hash key order -> no particular order).
• For each iteration through the loop, both key and value are advanced to the next key-
value pair in the dictionary.
• To do this, we first make a list of tuples where each tuple is (value, key). The items
method would give us a list of (key, value) tuples, but this time we want to sort by
value, not key. Once we have constructed the list with the value-key tuples, it is a simple
matter to sort the list in reverse order and print out the new, sorted list.