CodeGym /Courses /Python SELF EN /Advanced Work with Tuples

Advanced Work with Tuples

Python SELF EN
Level 8, Lesson 6
Available

15.1 Creating a Copy of a Tuple

So, we already know that tuples are immutable sequences of elements. Because they can’t be changed, making a copy of a tuple might seem like a minor task, since unlike lists, tuples remain the same after creation.

However, sometimes you might need to create a copy of a tuple, especially when it’s important to work with a separate version without changing the original data. Here are the main ways to do it:

Direct Assignment

The simplest way to create a copy of a tuple is to use direct assignment. This will create a new tuple object, but since tuples are immutable, both objects will reference the same data.


original_tuple = (1, 2, 3, 4, 5)
copied_tuple = original_tuple
print(copied_tuple)
        

In this case, copied_tuple will reference the same object as original_tuple.

Using the tuple() function

Another way to create a copy of a tuple is to use the built-in tuple() function. This function creates a new tuple by copying the elements of the original tuple.


original_tuple = (1, 2, 3, 4, 5)
copied_tuple = tuple(original_tuple)
print(copied_tuple)

        

This method is especially useful when you want to ensure you're creating a new object, even if the original object might be some other iterable.

Conversion via List

Sometimes you might need to create a mutable copy of a tuple in the form of a list, to make some changes, and then convert it back to a tuple.


original_tuple = (1, 2, 3, 4, 5)

temp_list = list(original_tuple)
temp_list.append(6)
copied_tuple = tuple(temp_list)
            
print(copied_tuple)

        

In this example, we first convert the tuple to a list, add a new element, and then convert the list back to a tuple.

15.2 Merging Tuples

Unlike lists, tuples cannot be changed after creation, but you can create new tuples by merging existing ones. Merging tuples is a common operation that can be useful in various programming scenarios.

Main ways to merge tuples

Concatenation

The simplest way to merge two or more tuples is to use the operator +. This creates a new tuple which is the result of concatenating the original tuples.


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
combined_tuple = tuple1 + tuple2
            
print(combined_tuple)
        

In this example, combined_tuple will contain the elements (1, 2, 3, 4, 5, 6).

Multiplication by an Integer

You can also use the * operator to multiply a tuple by an integer. This repeats the tuple a specified number of times and creates a new tuple.


tuple1 = (1, 2, 3)
multiplied_tuple = tuple1 * 3
            
print(multiplied_tuple)
        

Here multiplied_tuple will contain the elements (1, 2, 3, 1, 2, 3, 1, 2, 3).

The itertools.chain function

For merging a large number of tuples, you can use the chain function from the itertools module. This is handy when the number of tuples to merge is unknown beforehand.


from itertools import chain

tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
tuple3 = (7, 8, 9)
            
combined_tuple = tuple(chain(tuple1, tuple2, tuple3))
print(combined_tuple)
        

The result will be (1, 2, 3, 4, 5, 6, 7, 8, 9).

15.3 Nested Tuples

Tuples that contain other tuples inside them are called nested tuples. Nested tuples offer a convenient way to organize and structure data, especially when you need to represent multidimensional data or complex structures.

You can merge tuples to create nested structures. This is useful for organizing data into multidimensional structures. Creating a nested tuple in Python is as simple as creating a regular tuple. You just include one tuple inside another.


nested_tuple = ((1, 2, 3), (4, 5, 6), (7, 8, 9))
        

In this example, nested_tuple contains three tuples, each of which consists of three elements.

Example of creating a nested tuple from regular ones:

In this example, nested_tuple contains three tuples, each of which consists of three elements.


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
nested_tuple = (tuple1, tuple2)
            
print(nested_tuple)
        

Here nested_tuple will contain ((1, 2, 3), (4, 5, 6)).

Accessing Elements of a Nested Tuple

Accessing elements of a nested tuple is done using multiple indexing. You first specify the index for the outer tuple, and then for the inner tuple.


nested_tuple = ((1, 2, 3), (4, 5, 6), (7, 8, 9))

# Access element 5 in the second nested tuple
element = nested_tuple[1][1]
print(element)  # Output: 5
        

Multidimensional Arrays

Nested tuples are often used to represent two-dimensional or multidimensional arrays. For example, a data table can be represented as a tuple of tuples, where each row of the table is a separate tuple.

Structuring Data

Nested tuples are handy for structuring data, like geographical coordinates (latitude and longitude) for multiple locations:


locations = (("New York", (40.7128, -74.0060)), 
             ("Los Angeles", (34.0522, -118.2437)), 
             ("Chicago", (41.8781, -87.6298)))
        
1
Survey/quiz
Tuples in Python, level 8, lesson 6
Unavailable
Tuples in Python
Tuples in Python
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION