Python’s tuple is an ordered and immutable collection of items. Tuples, unlike lists, can have their values changed after they’ve been created. Tuples are defined in Python by surrounding the items in parenthesis ( ). A tuple’s components do not have to be of the same type.
Characteristics of a Tuple
- It’s Ordered (Indexed): Tuple elements are kept in order – with an index or position – in the collection, and their place in the collection is permanent, and they can be retrieved by their position.
- It Can’t Be Changed (Or Immutable): A tuple’s elements can’t be moved, deleted, added to, or changed after it’s been created.
- Duplicates are permitted: As tuples are indexed, duplicate values are permitted, allowing numerous elements with the same value to be stored at various indexes without conflict.
- It’s used to keep a set of data that doesn’t change: A comma-separated collection of values in brackets is used to define a tuple.
A tuple’s basic syntax is as follows:
How to Create a Tuple in Python
Tuples are made by encircling objects with () and separating them with commas (,).
Consider the following examples:
We’ve created four tuples in this example: an empty tuple, a tuple with a single item, a tuple with numerous items, and a tuple with distinct categories of objects.
Accessing Items in a Tuple
You must write an index number inside the square bracket to access items in a tuple.
The index numbers begin at zero (0).
Here are some examples of ways to get elements into a tuple:
They’ll all produce the output below respectively:
How to modify a tuple in Python
Unlike a Python dictionary or a list, we know that a tuple is an immutable data type. That is, we cannot change a tuple in any way. However, there are times when we may need to change a tuple. We have no other choice except to generate a new tuple with the desired members in some scenarios. Here, we’ll change a tuple using various methods such as slicing, packing, and unpacking.
What is the best way of appending elements to a tuple?
To append an element to a tuple’s end by using tuple concatenation, much like appending a string to another string with string concatenation. To accomplish so, we’ll first create a tuple with a single element to which the tuple will be attached. Then we’ll concatenate the new and existing tuples as follows, using the + operator.
The output will be
Using the above method, we can indeed append an element to the beginning of the tuple, as shown below.
The output can be seen as follows:
We can also use packing and unpacking with the * operator to append elements to the tuple. We’ll start by using the * operator to unpack the existing tuple. After that, we’ll make a new tuple with the new element, as seen below.
The output can be seen below:
In the same manner that we append an element to the end of a tuple, we can also append an element to the start of a tuple using packing and unpacking.
This will also give the output below:
Inserting an element in a tuple at a given location
Slicing can be used to insert an element at a precise location in a tuple. If we need to insert an element at index i of the tuple, we’ll slice it and make two new tuples. The first tuple will contain entries from the original tuple’s index 0 to i-1.
The elements from index I to last will be in the second tuple. Then, at index i, we’ll generate a tuple containing a single element that must be entered into the tuple. Then, as seen below, we’ll concatenate the three tuples to insert the new element at index “i.”
To merge the newly created tuples, we can utilize packing and unpacking as shown below:
Both of these will produce the following output:
Modifying a tuple by deleting an element
To remove one element from the beginning of a tuple, we’ll create a new tuple with the remaining members, as seen below.
This will produce the output:
If we need to remove a tuple’s last element, we can do it by slicing out the remaining elements as shown below.
The output is seen below:
Some of Python’s built-in functions for Tuples
Tuples have a number of built-in functions that reduce the number of lines of code needed. Let’s take a look at some of the features that tuples provide.