In this Python Tuples article we want to talk about 5 Examples of How to Use Python Tuples in your Code, so Python tuples are powerful and useful data structure that can be used in different ways. in this article we want to talk about 5 examples of how to use Python tuples in your code.
1. Returning Multiple Values from a Function
One of the most common use cases for tuples is to return multiple values from function. for example let’s say we have a function that performs some calculation and we want to return both the result of the calculation and the amount of time it took to perform the calculation. we can use a tuple to return both values
1 2 3 4 5 6 7 8 9 10 11 12 |
import time def perform_calculation(): start_time = time.time() # perform some calculation here end_time = time.time() result = 42 return result, end_time - start_time result, time_taken = perform_calculation() print("Result:", result) print("Time Taken:", time_taken) |
In the above example perform_calculation() function returns a tuple containing the result of the calculation and the time taken to perform it. after that we unpack the tuple into separate variables using the syntax result, time_taken = perform_calculation().
This will be the result
2. Grouping Related Data Together
Tuples can also be used to group related data together. for example let’s say we have a list of 2D points and we want to calculate the distance between each pair of points. we can represent each point as tuple containing its x and y coordinates.
1 2 3 4 5 6 7 8 9 10 |
import math points = [(0, 0), (1, 1), (2, 2), (3, 3)] for i in range(len(points)): for j in range(i+1, len(points)): x1, y1 = points[i] x2, y2 = points[j] distance = math.sqrt((x2-x1)**2 + (y2-y1)**2) print("Distance between", points[i], "and", points[j], "is", distance) |
This will be the result
3. Immutable Keys in Dictionaries
Python Tuples can be used as keys in dictionaries because they are immutable. this means that once a tuple is created, its contents cannot be changed. let’s say we have a dictionary that maps employee IDs to their names and salaries. we can use tuples to represent each employee name and salary.
1 2 3 4 5 6 7 |
employee_dict = { (1, "GeeksCoders"): 50000, (2, "Bob"): 60000, (3, "Charlie"): 70000 } print("Employee 1 is", employee_dict[(1, "GeeksCoders")]) |
This will be the result
4. Sorting Sequences
Python Tuples can also be used to sort sequences. let’s say we have a list of 2D points and we want to sort them by their distance from the origin.
1 2 3 4 5 6 7 8 9 10 |
import math points = [(3, 3), (1, 1), (2, 2), (0, 0)] def distance_from_origin(point): return math.sqrt(point[0]**2 + point[1]**2) sorted_points = sorted(points, key=distance_from_origin) print("Sorted Points:", sorted_points) |
This is the result
5. Packing and Unpacking Arguments
Tuples can also be used to pack and unpack function arguments. for example let’s say we have a function that takes three arguments.
1 2 3 4 |
def my_function(a, b, c): print("a:", a) print("b:", b) print("c:", c) |
We can call this function using tuple to pack the arguments.
1 2 |
my_tuple = (1, 2, 3) my_function(*my_tuple) |
In this example we have used * operator to unpack the tuple into separate arguments when calling my_function().
This will be the result