Tuples

Tuples

Let’s look back to tuples and dive deeper into a topic we briefly brushed in an earlier blog. Consider the following:

tuple_variable = ("Laptops", "Mobiles", "Game Consoles", "Television", "Smart Watches")

Accessing items in a Tuple

An item within a tuple can be accessed the same way we would access a list’s item.

print(tuple_variable[2])  # Prints “Game Consoles”

In the same manner, we can select the last item and a range of items from a Tuple, in the same way.

print(tuple_variable[-1])  # Prints "Smart Watches"
print(tuple_variable[1:4])  # Prints "Mobiles", "Game Consoles", "Television"

Tip:

In Python ‘type casting’, is a concept that is used by a programmer to explicitly convert one data type to another. To convert a variable’s data type, we follow the syntax shown below:

  • data_type_name(variable_name)

Let’s look at this with an example, consider the following example.

x = 5

print("Value in x before changing:", x, "type of x before changing:", type(x))

x = str(x)

print("Value in x after changing:", x, "type of x after changing:", type(x))

This should give the output:

As you can see, we first created a variable x, that holds a value of type ‘int’, we then converted it to a string, and then when we printed the type of x, we can see it is of type ‘str’.

Storing a single item in a Tuple

You might be thinking this is a purely simple task, based on what we learned so far all we have to do is create a tuple with a single item. While this will not raise an error, you will be surprised when we see the type of item we created. 

Try this as an exercise, create a variable with a single item in a tuple and then check its type. Once you’ve tried it, then follow on below to learn how to achieve this.

tuple_1 = ("Tuple Test 1")
print(type(tuple_1))  # Type 'str'

tuple_2 = ("Tuple Test 2",)
print(type(tuple_2))  # Type 'tuple'

The accurate way of creating a tuple with only one variable is by adding a comma, even if there is only one item inside the tuple.

List against Tuple

The main difference between a List and Tuple is that items in a List are mutable (changeable) whereas the items of a Tuple are immutable (unchangeable). 

Technically Lists are mutable thus an extra memory block is assigned to it when created to increase the size of the list if required. However when Tuples are created no such block is assigned, as Python just needs to assign the minimum memory. Thus it makes Tuples to be more memory efficient than Lists and this also offers Tuples the advantage of being slightly faster than Lists.

The difference between when to use a List or a Tuple is if we want to store data to which we don’t want to add or modify or remove data, we should use Tuples.

Immutability of a Tuple

The immutability of a Tuple can be overturned using the following method:

We can convert a tuple to a list, then modify it by changing or adding, or removing an item, and then converting it back to a tuple.

tuple_variable = ("Laptops", "Mobiles", "Game Consoles", "Television", "Smart Watches")




tuple_variable = list(tuple_variable)  # Converting to type List

print(tuple_variable)  # No changes to the items
print(type(tuple_variable))  # Print the list

tuple_variable.append("VR")  # Adds item "VR"

print("")
print(tuple_variable)  # Updated list

tuple_variable = tuple(tuple_variable)  # Converting to type tuple

print("")
print(tuple_variable)
print(type(tuple_variable))

This gives us the output:

Let’s quickly break down the above code snippet. We first created our tuple collection, and then on the next line of code, we converted it to a list. To verify that the items are intact, we printed it out to see the items in the list, then the type of the variable as well. We then used the append() function to add an item to the list and then to see the updated list we printed out the contents of the list. Finally, we then converted the variable back to a tuple, then printed out its contents and the type of the variable as well.

Extracting items from a Tuple

We can extract the items of a tuple in the following way:

vegetable_tuple = ("Carrots", "Beans", "Potatoes")

(veggie_1, veggie_2, veggie_3) = vegetable_tuple

print("Veggie 1 is", veggie_1)
print("Veggie 2 is", veggie_2)
print("Veggie 3 is", veggie_3)

What have we learned?

  • What are the different ways to access items of a Tuple?
  • What is typecasting?
  • What is the correct way of storing a single item in a Tuple?
  • How can we bypass a Tuple’s property of immutability?
  • How do we unpack a Tuple?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments