1. Home
  2. Docs
  3. Python 101 – Python Programming for Beginners
  4. Tuples in Python

Tuples in Python

 
Tuple is like list, but it is immutable.

It’s an immutable sequence.

 

How tuple is created ?

 
A tuple is usually defined by using ( ) brackets.
 
But ( ) brackets are not mandatory
 

>>>empty_tuple = ()

'empty_tuple' is a reference to an empty tuple

>>>empty_tuple

>>>()

 

Here is an example of tuple with numbers.

>>>num_tuple = (10,20,30,40,50)

>>>num_tuple

(10, 20, 30, 40, 50)

 

  
Tuple can contain any objects(heterogeneous).
 
Here mixed_tuple contains, integer, float, string, a list, a tuple
 
Notice tuple is defined without using () brackets.
  


>>>mixed_tuple = 1,1.0,"Hello",[10,20,30,40,50],(8,9,10)

>>>mixed_tuple

(1, 1.0, 'Hello', [10, 20, 30, 40, 50], (8, 9, 10))


 
 

A tuple can also be created by using built-in tuple constructor function.
 

tuple(iterable)

  
Returns a tuple from iterable object.
 
tuple(range(0,10))
 

tuple form a range object

  

>>>t1 = tuple(range(0,10))

>>> t1
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

  

tuple from a list object

  

>>>t2 = tuple([10,20,30,40,50])

>>>t2

(10, 20, 30, 40, 50)

  

Common Operations on Tuples

 

Indexing and slicing

  

Indexing

 
How to access Tuple elements ?
 
Like other sequence, elements of tuple can be accessed by indexing.
 
index are used inside [] square brackets, to access elements.
  

>>>num_tuple[0]

10

>>>num_tuple[1]

20

>>>num_tuple[2]

30

>>>num_tuple[3]

40

  
Tuple is immutable. Which means you can not, change the values of the tuple.
  
Example.
  

>>>mixed_tuple

(1, 1.0, 'Hello', [10, 20, 30, 40, 50], (8, 9, 10))

 

  
Let’s try to change the value of an element
 

Ex 1:

Trying to change first element form 1 to 10

  

>>>mixed_tuple[0] = 10

output:

Traceback (most recent call last):

File "", line 1, in

TypeError: 'tuple' object does not support item assignment

 

  

But if the element of tuple, is mutable its value can be changed.

In this example mixed_tuple contains a list,

As you know, list is mutable, so lets try to change its value.

How to access list inside a tuple ?

List is forth element in tuple, so it’s index is 3.

  

>>> mixed_tuple[3]

[10, 20, 30, 40, 50]

 

  

How to access value of a list inside a tuple?

 

list value can be accessed by two dimensional indexing.
 

>>>mixed_tuple[3][0]

10

>>>mixed_tuple[3][2]

30

 

  

Now let’s change 3rd value of the list.

  

>>>mixed_tuple[3][2] = 100

>>>mixed_tuple[3][2]

100

 

  

Now check the whole tuple,

notice of value 3rd element of list, changed from 30 to 100

  

>>>mixed_tuple

(1, 1.0, 'Hello', [10, 20, 100, 40, 50], (8, 9, 10))

 

  

Accessing tuple elements backwards

 
You can also use negative index to access elements of tuple form backwards.
  

>>>mixed_tuple[-1]

(8, 9, 10)

>>>mixed_tuple[-3]

'Hello'

 

  

>>> mixed_tuple[-2]

[10, 20, 30, 40, 50]

 

  

Slicing

  

Like in lists , strings [:] operator is used to slice a tuple.
 
Lets look at another example of tuple with different values.

  

t1 = ('John', 1.2, 10, 'hello', 3, 4, 5, 19, 203, 10, 1)

>>>t1[4:]

('John', 1.2, 10, 'hello', 3, 4, 5, 19, 203, 10, 1)

>>> t1[:-3]

(10, 203, 1, 'hello', 'John', 1.2, 10, 'hello', 3, 4, 5, 19)

>>>t1[5:-3]

(1.2, 10, 'hello', 3, 4, 5, 19)


>>>t1[::-1]

(1, 10, 203, 19, 5, 4, 3, 'hello', 10, 1.2, 'John', 'hello', 1, 203, 10)

>>>t1[::-2]

(1, 203, 5, 3, 10, 'John', 1, 10)

 

  

Below you will see some examples of extracting values from tuple

and assigning them to variables using slicing method.

Assignment examples with slicing

 
We can assign values to variables, by slicing.
 
Have a look at below examples.
  

Ex 1.
 

In this example we want to assign a string value, a tuple and a list to variables
 
a, t, lst respectively from a tuple, (“0123456789”,(‘one’,’two’,’three’),[1,2,3,4,5])

As said above the tuple contains a string, tuple, and list as values.

We will try to get full or part of each value.

 
Ex 1:
 
Assigning full values to a,t,lst

  

>>>a,t,lst = "0123456789"[:],('one','two','three')[:],[1,2,3,4,5][:]

>>> a

'0123456789'

>>>t

('one', 'two', 'three')

>>>lst

[1, 2, 3, 4, 5]

 

  

Ex 2:
 
Partial values assigned to a, t, lst
  

>>>a,t,lst = "0123456789"[:3],('one','two','three')[:3],[1,2,3,4,5][2:]

>>>a

'012'

>>>t

('one', 'two', 'three')

>>>lst

[3, 4, 5]

 

  

Significance of ( ) brackets

Event though ( ) required while defining tuple,
 
( ) mandatory when passing tuple as an argument.

Example :

Let’s consider a function which takes tuple as argument. Learn about functions.

Here in this example func() is a function, takes ‘t’ tuple as argument.

It just prints the tuple, nothing else.

def func(t):

    print(t)

 

  

Now pass lets say we have tuple

t1 = 1,2,3,4,5

We want to pass it to func(), without ( ) parenthesis.

    
func(1,2,3,4)


output:

TypeError                                 Traceback (most recent call last)
<ipython-input-32-3b93d76374df> in <module>()
      2     print(t)
      3 
----> 4 func(1,2,3,4)

TypeError: func() takes 1 positional argument but 4 were given

 

  

Notice you’re presented with a TypeError,

Here Python treats value of tuple as individual arguments.

So its mandatory to pass tuple with ( ) brackets.

Let’s do that, pass the tuple with ( ) brackets.

 

    
func((1,2,3,4))

output:

(1, 2, 3, 4)

 

  

 
Now the output is as we expected.
 

Built-in functions on tuple

  
help() and dir() on tuple.
 
You can get more information tuple implementation by help() function.
 

help()

 

 

t1 = ()

help(t1)

 

  
type help(t1) on Python interpreter and check,

information of tuple() implementation.
 

dir(t1)

  
Lists tuple names comprising its attributes.
 

Try it yourself to find dir() on tuple, you will notice

there are very few methods available as compared to list.

Because Tuple is immutable, append(), insert(), extend()

etc… methods are not available.

 

Using Built-in functions on Tuple

 

len(), max(), min()

 

len(iterable)

 
Returns the length of the tuple.
 
t1 is tuple with arbitrary values.
  

>>>t1 = (10,203,1,"hello", "John",1.2,10,"hello", 3,4,5,19,203,10,1)

>>>t1
(10, 203, 1, 'hello', 'John', 1.2, 10, 'hello', 3, 4, 5, 19, 203, 10, 1)

>>>len(t1)
15

 

  

max()

 
Returns max value in tuple of type elements.
  

min()

  
Returns min value in tuple of same type elements.
  

Below are some examples of these.
 
Ex 1:
 

Maximum value in tuple of numbers.

  

>>>a = 10,200,90,39,55

>>> max(a)

200

 

  

Ex 2:

Maximum value character in sequence.

  

>>>c = "justlearnpython"

>>>max(c)

'y'

 

  

Ex 3:
 
Minimum value of tuple of numbers
  

>>>min(a)

10

 

  

Ex 4:

Minimum value in string.
  

>>>min(c)

'a'

 

  
If you try to use max and min tuple with heterogeneous objects.
 
You will get a TypeError, as bellow.
  

t1 = (10,203,1,"hello", "John",1.2,10,"hello", 3,4,5,19,203,10,1)

>>>max(t1)

Traceback (most recent call last):
File "", line 1, in
TypeError: < not supported between instances of 'str' and 'int'


>>>min(t1)

Traceback (most recent call last):
File "", line 1, in
TypeError: < not supported between instances of 'str' and 'int' 


 

  

  

Tuple methods

  

count()

  
Returns number of occurrences of a value in a tuple.
  

>>>t1 = (10,203,1,"hello", "John",1.2,10,"hello", 3,4,5,19,203,10,1)

>>>t1.count(10)

3

>>>t1.count("hello")

2

>>>t1.count(203)

2

>>>t1.count(5)

1

  

index()

 
Returns first index of a value.
  

>>>t1.index(10)

0

>>>t1.index('hello')

3

>>>t1.index(1)

2

>>>t1.index(19)

11

 

  

That’s pretty much covers the basics of Tuple, try out Tuple Exercises.