Python: Tuples

In Python, a tuple is an ordered sequence of fixed or immutable values, which are often heterogeneous. And unlike the square brackets in lists, tuples are enclosed with parentheses "()".

This is how we define a tuple in Python.

				
					dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
					print(dogtuple)
				
			

The print() function will output the following:

				
					('beagle', 'dalmatian', 'dobermann', 'greyhound')
				
			

Accessing Tuple Items

python tuple

Indices in tuples start from 0. As with lists, they can also be accessed by enclosing the index within square brackets. Here we access and print out the third element (index 2) of the tuple

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						print(dogtuple[2])
					
				

which gives

					
						dobermann
					
				

Negative indices count from the end of the tuple, from the right. Negative indices start from -1. The code below

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						print(dogtuple[-1])
					
				

prints out

					
						greyhound
					
				

We can also slice out the elements of a tuple within a specified range. Below we create a new tuple by extracting the elements of the given tuple from index 1 to 3 with the expression [1:3], excluding the last element with index 3.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						print(dogtuple[1:3])
					
				

The above script gives the output

					
						('dalmatian', 'dobermann')
					
				

The range works the same for the -ve range also. Below we slice the elements from index -3 to -1 (which is exluded)

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						print(dogtuple[-3:-1])
					
				

Altering/Changing the Vaue of a Tuple Item

As mentioned in the beginning of the tutorial, items in a tuple are unchangeable. Below we try to change/modify the third element in the tuple dobermann, and assign it a different value.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						dogtuple[2] = "rottweiler"
						print(dogtuple)
					
				

It however ends up throwing an error stating that 'tuple' object does not support item assignment.

python tuple assignment error

There however is a workaround to achieve this. The tuple is first converted to list, then the intended item is modified and the list is converted back to tuple.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						y = list(dogtuple)
						y[2] = "rottweiler"
						dogtuple = tuple(y)

						print(dogtuple)
					
				

The third item is changed in the output.

					
						('beagle', 'dalmatian', 'rottweiler', 'greyhound')
					
				

Length of a Tuple

To find the number of items in a tuple, use the len() built-in function.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")			
						print(len(dogtuple))
					
				

This script will print out

					
						4
					
				

Looping Through a Tuple

The for in statement is used to loop through each element in a tuple.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")			
						for x in dogtuple:
							print(x)
					
				

The above iteration will print the following

					
						beagle
						dalmatian
						dobermann
						greyhound
					
				

Checking If An Item Exists

To check if an item exists in the tuple or not, the if in statement is used.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")				
						if "beagle" in dogtuple:
							print("beagle exists")
					
				

Appending/Adding Items to a Tuple

Trying to add extra items into an existing tuple also will throw an error. Below, we try to add the fifth item into the tuple, which results in error.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						dogtuple[4] = "rottweiler"
						print(dogtuple)
					
				

Deleting/Removing Tuple

Since items are immutable in a tuple, we cannot remove them directly. To do so, we have to take the similar approach of converting the tuple into a list first, then remove the item, and convert it back to a tuple. We can however delete an entire tuple.

A tuple can be deleted using the del keyword. Below, the print statement causes an error as the tuple being printed is already deleted in the preceding statement.

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")
						del dogtuple
						print(dogtuple)
					
				

Joining Two Tuples

The + operator is used to join or concatenate two or more tuples. Below we join two tuples dogtuple and anothertuple

					
						dogtuple = ("beagle", "dalmatian", "dobermann", "greyhound")			
						anothertuple = ("rottweiler", "pug")
						newtuple = dogtuple + anothertuple
						print(newtuple)
					
				

and get a new tuple.

					
						('beagle', 'dalmatian', 'dobermann', 'greyhound', 'rottweiler', 'pug')
					
				

The Tuple Constructor

The tuple() construtor allows you to create a tuple from an existing collection or sequence

					
						dogtuple = tuple(("beagle", "dalmatian", "dobermann", "greyhound"))				
						print(dogtuple)