Python Tuple in Depth

diving into python tuple

Content:

Introduction

Advantages

Basic Operations

Magic

How to Boost Performance with Tuples

Conclusion

Introduction

Tuple is one of python data types that categorize as sequence and it consists of comma-separated objects like string, number, list and even another tuple. Tuple is immutable, and that’s differ it from list. Here is some example to create a tuple:

# Example 3 - trick for create a tuple that consists only one object.>>> a = "1">>> y = (a)>>> type(y)str>>> a = "1">>> y = (a,)>>> type(y)tuple>>> print(y) # When you print it will show the comma('1',)>>> print(y[1]) # But when you want to get the blank after comma, it will throw an error. So don't worry, it still consists of one object.IndexError: tuple index out of range>>> len(y)1

Basic Operations

Accessing Values

We can access the values by using their index directly or slicing. Basically we just play with index values inside the square brackets. The index will start at zero and ended with n-1, where n is the length of tuple. Access via slicing will be different because it use this format “var[from:until+1]”. Here some example of accessing values in tuple:

We can concatenate tuples. Wait, what!? Tuple is immutable right? The answer is YES. Tuple is immutable, when we concatenate two or more tuples, it will have different id. That mean, we create a new tuple that contain tuple A + tuple B. Let see the example below:

Multiply mean that we duplicate n times of the tuple. When you have a tuple consists of string (“A”). Then multiply it by two will make the tuple contains (“A”, “A”). The concept is same with concatenation, it will create a new tuple. Here is the example of tuple multiplying:

>>> x = ("A",)>>> x = x * 2>>> print(x)('A', 'A')

Delete Tuple

Since it was immutable, so we can’t delete an item in tuple. What we can do is delete the whole tuple. To delete a tuple, simply use “del”. Here the example:

There is some ways to compare tuples. The first one is use “is”, it will compare tuples by their id. When a tuple have same id, it must have same items too. Then the second is “==”, this only compare tuples by their items. When tuples have same items then return true, same items doesn’t mean have same id. I will try to show you the different:

Notes: you must have a tuple with values that have same data types, you can’t compare number with string.

Sort item in tuple

Then, how to sort? Are this can be easily achieved? Yes. With the power of “sorted”, we can easily sort values in tuple ascending or descending. Default sorting type is ascending, add “reverse=True” when we want to sort it descending.

Magic

Aha! Its changed. Tuple is immutable, but its only the structure. Its consists of object id references and if the object referenced is mutable, then that object can be changed. Please check the code and image below to see how its work:

When a function called, we can save the input parameter and result. Then if we call that function with same parameter, it will get the result directly from dictionary. So, we didn’t need to recalculate same result.

Conclusion

Tuple maybe a simple and forgotten python’s data type. But, its have a lot of great features. So, lets start to use and discover more. If you want to read my article about variables, then go here. Claps if this article help you and feel free to ask me.

Data Driven Investor (DDI) brings you various news and op-ed pieces in the areas of technologies, finance, and society. We are dedicated to relentlessly covering tech topics, their anomalies and controversies, and reviewing all things fascinating and worth knowing.

Never miss a story from Data Driven Investor, when you sign up for Medium. Learn more