Python Tuples

A Tuple is like a List which is a member of data types called  sequences.

Tuples and Lists are almost similar to each other, the only difference between them is that unlike Lists,

Tuples are immutable, i.e., elements in a Tuple cannot be changed.

Tuples have some advantages over Lists even though they are very similar to each other.

Few advantages of Tuples over Lists are given below: –

  • As Tuples are immutable, they are more secure than Lists. This prevents accidental modification of Tuple members.
  • The iterator reads a Tuple faster than a List because of their immutable property, this saves some time in execution process.
  • Dictionary is another Python data type and it requires one of its component values to be immutable and Tuple can be used for this. On the other hand, a List is mutable and thus cannot be used.

Creating A Tuple

Tuples are created by placing all the elements in enclosed small brackets () separated by commas. 

A Tuple can contain multiple other Tuples and Lists inside it, thus creating a nested system.

Example

Python - creating a tuple

Similarly, a Tuple can also be created by enclosing all the items in a constructor called the Tuple constructor.

Example

T = tuple((1, 2, 3, 4, 5, 6))
print(T)

Accessing Items in a Tuple

Each element in a Tuple is associated with its own index number and can be accessed by using that number. 

The number is put inside closed square brackets [ ] with the Tuple’s name.

Example

Python - invalid indexing error

Nested elements can be accessed using the same indexing method. 

Elements in a string, i.e., sub-strings can also be accessed by this method. Let see how

Example

Python- Nested string & substring

The number which comes in the first square brackets represents the element of the main Tuple and the second brackets’ index number indicates to the sub-members of the nested members. 

Python interpreter will show an error if there are no nested members present and still this syntax is used.

Example

Python-no-submember-error

Operation on Tuples

You can also use Tuples as operands to different operators like the addition (+) and the multiplication (*) operators. 

Concatenation can be performed on two tuples by using the addition operator. Also, with the help of a multiplication operator, you can make a Tuple to repeat itself just like Lists.

Example

Python- Concatenation using addition

Similarly, to make a Tuple repeat itself, we have to do this:

Example

Python- Repeat tuple using multiplication

If you check the data type of a Tuple by using the type() method, Python will obviously will identify as a Tuple but when you store only a single element in it, the output will be the data type of that element.

Example

Python- type() method to check data type

Python identified it as an integer because Python just ignored the small brackets because they are also used to define some operator’s priority in mathematical expressions. 

This also applies for all the data types. To make Python “believe” that the value stored in the variable is actually a Tuple, the integer (or any other data type) should be followed by a Comma.

Example

Python- Comma value tuple

Looping Through a Tuple

The for loop can be used to loop through a Tuple like this: –

Example

Python- for loop

Indexing

Elements in a Tuple can be accessed by their unique index number. This is called Indexing

The element of the first element is zero which is similar to Lists and Strings. Let’s take an example

Example

Python Indexing

If you want to access some element on an index number that does not exist, Python interpreter will show an error.
Similar to Lists, the elements in the nested data types can also be accessed in Tuples.

Also, if there are no elements in a nested member, the code won’t run.

Negative Indexing

Indexing can also be done using negative numbers.

This works in a whole opposite manner, and instead of starting with 0, the index number starts with the negative of the last positive index number and the index of the last element becomes -1.

Have an example to understand this much better: –

Example

Python - Negative indexing

Bonus Tips
This is very useful when you have a very long Tuple and you don’t know the size of it. With the help of negative indexing you can access some last elements in the Tuple.

Slicing

Slicing is used to access a range of elements in a sequence, in this case the sequence is a Tuple.

This method can also be used to print the whole Tuple in a reverse order and also to print some parts of it in the reverse order.

Example

Python- Slicing Tuple

Updating and Deleting a Tuple

Tuples are immutable which means that they cannot be changed.

Updating them simply by using the assignment operator like this will result in an error –

Example

Python-update tuple-error

However, Tuples can be converted into lists then can be updated or changed.

After the updating is done, you can convert the List back into a Tuple.

Example

Python- Update tuple using list

Also, you cannot delete elements from a Tuple. However, the whole Tuple can be deleted by using the del function.

Example

Python- Tuple del function

Tuple Method

index() – Searches the specific element in the Tuple and returns its index number.

count() – It returns the times an element occurs in the Tuple.

Tutorials for all brains!