How To Return A Tuple In Python [Explained]

To return a tuple in Python, you simply enclose the values you want to return within parentheses, separated by commas. These values can be of any data type, and you can create a tuple explicitly or dynamically within a function.

Here’s an example of how to return a tuple from a function:

def return_tuple():
    # Create a tuple with some values
    my_tuple = (1, 2, 3, 4)
    
    # Return the tuple
    return my_tuple

# Call the function and store the returned tuple in a variable
result_tuple = return_tuple()

# Access elements of the returned tuple
print(result_tuple[0])  # 1
print(result_tuple[1])  # 2
Code language: Python (python)

In this example, the return_tuple function creates a tuple (1, 2, 3, 4) and returns it. You can then call this function and store the returned tuple in a variable for further use.

You can also return a tuple with values that are computed within the function, like this:

def calculate_values():
    a = 10
    b = 20
    c = a + b
    d = a - b
    
    # Return a tuple with the computed values
    return (a, b, c, d)

result_tuple = calculate_values()

# Access elements of the returned tuple
print(result_tuple[0])  # 10
print(result_tuple[1])  # 20
print(result_tuple[2])  # 30
print(result_tuple[3])  # -10
Code language: Python (python)

In this case, the function calculate_values computes a, b, c, and d, and returns them as a tuple.

Example 2

To return a list of tuples in Python, you can create a list where each element is a tuple. Here’s an example:

def return_list_of_tuples():
    # Create a list of tuples
    list_of_tuples = [(1, 'apple'), (2, 'banana'), (3, 'cherry')]
    
    # Return the list of tuples
    return list_of_tuples

# Call the function and store the returned list of tuples in a variable
result_list = return_list_of_tuples()

# Access elements of the returned list of tuples
print(result_list[0])  # (1, 'apple')
print(result_list[1])  # (2, 'banana')
print(result_list[2])  # (3, 'cherry')Code language: Python (python)

In this example, the return_list_of_tuples function creates a list of tuples and returns it. Each element of the list is a tuple containing two values.

You can also construct a list of tuples where each tuple is generated dynamically within the function, like this:

def generate_list_of_tuples():
    list_of_tuples = []
    
    for i in range(1, 4):
        list_of_tuples.append((i, i * 2))
    
    return list_of_tuples

result_list = generate_list_of_tuples()

# Access elements of the returned list of tuples
print(result_list[0])  # (1, 2)
print(result_list[1])  # (2, 4)
print(result_list[2])  # (3, 6)Code language: Python (python)

In this case, the function generate_list_of_tuples uses a loop to generate tuples and adds them to the list, and then returns the list of tuples.

Returning a tuple in Python provides an elegant way to bundle and convey multiple values from a function.

This not only simplifies your code but also makes it more readable and organized. Whether you are returning coordinates, status information, or any related set of data, tuples allow you to maintain data integrity while conveying multiple pieces of information as a single unit.

So, remember to use tuples when you need to return or work with collections of values that should remain unaltered throughout your program.

Read More;

    by
  • Aniket Singh

    Aniket Singh holds a B.Tech in Computer Science & Engineering from Oriental University. He is a skilled programmer with a strong coding background, having hands-on experience in developing advanced projects, particularly in Python and the Django framework. Aniket has worked on various real-world industry projects and has a solid command of Python, Django, REST API, PostgreSQL, as well as proficiency in C and C++. He is eager to collaborate with experienced professionals to further enhance his skills.

Leave a Comment