In this post, you will learn the Feature, Uses, and Advantages of TUPLE in Python. Before we get started, if you want to String Formatting in the python program, please go through the following article: String Formatting In Python.

What is Tuple in Python?

Tuple in Python is a method of grouping the tuples by matching the second element in the tuples. It is achieved by using a dictionary by checking the second element in each tuple in python programming. However, we can make new tuples by taking portions of existing tuples.

Basic Syntax of Tuple in Python

To write an empty tuple, you need to write as two parentheses containing nothing-

For writing tuple for a single value, you need to include a comma, even though there is a single value. Also in the end you need to write semicolon as shown below.

Tuple indices begin at 0, and they can be concatenated, sliced, and so on.

Packing and Unpacking

In packing, we place value into a new tuple while in unpacking we extract those values back into variables.

Comparing tuples

A comparison operator in Python can work with tuples. The comparison starts with the first element of each tuple. If they do not compare to =,< or > then it proceeds to the second element and so on. It starts by comparing the first element from each of the tuples. Let’s study this with an example-

#case 1

#case 2

#case 3

Case1: Comparison starts with the first element of each tuple. In this case 5>1, so the output a is bigger

Case 2: Comparison starts with the first element of each tuple. In this case 5>5 which is inconclusive. So it proceeds to the next element. 6>4, so the output is bigger

Case 3: Comparison starts with the first element of each tuple. In this case 5>6 which is false. So it goes into the else block and prints “b is bigger.”

Using tuples as keys in dictionaries

Since tuples are hashable, and the list is not, we must use a tuple as the key if we need to create a composite key to use in a dictionary.

Example: We would come across a composite key if we need to create a telephone directory that maps, first-name, last-name, pairs of telephone numbers, etc. Assuming that we have declared the variables as last and the first number, we could write a dictionary assignment statement as shown below:

Inside the brackets, the expression is a tuple. We could use tuple assignment in a for loop to navigate this dictionary.

This loop navigates the keys in the directory, which are tuples. It assigns the elements of each tuple to last and first and then prints the name and corresponding telephone number.

Tuples and dictionary

Dictionary can return the list of tuples by calling items, where each tuple is a key-value pair.

Deleting Tuples

Tuples are immutable and cannot be deleted. You cannot delete or remove items from a tuple. But deleting tuple entirely is possible by using the keyword

Slicing of Tuple

To fetch specific sets of sub-elements from tuple or list, we use this unique function called slicing. Slicing is not only applicable to tuple but also for array and list.

The output of this code will be (‘c’, ‘d’).

Here is the Python 2 Code for all the above example

Built-in functions with Tuple

To perform different task, tuple allows you to use many built-in functions like all(), any(), enumerate(), max(), min(), sorted(), len(), tuple(), etc.

Advantages of tuple over the list

  • Iterating through a tuple is faster than with a list since tuples are immutable.
  • Tuples that consist of immutable elements can be used as a key for the dictionary, which is not possible with a list
  • If you have data that is immutable, implementing it as a tuple will guarantee that it remains write-protected

Next Recommended Article: How To Use String Formatting In Python

The article was published on September 19, 2020 @ 4:01 PM

Leave a Comment