python logo

python tuple


Python hosting: Host, run, and code Python in the cloud!

The tuple is an essential data structure in Python, used to store a collection of related data. Different from lists, once a tuple is created, its values remain immutable.

Recommended Course: Python Programming Bootcamp: Go from zero to hero

What is a Python Tuple?

A tuple allows you to store multiple items in a single structure. Here’s how to define an empty tuple:

tuple_example = ()

For a tuple containing just one item, you need a comma after the item:

single_item_tuple = (3,)

This might seem odd at first, but omitting the comma can cause unexpected behavior. For tuples with multiple items, there’s no need for a trailing comma:

personInfo = ("Diana", 32, "New York")

A tuple can accommodate diverse data types—both text and numbers can coexist within the same tuple.

Accessing Tuple Data

Like lists, tuples are indexed. You can retrieve any item by referencing its index:

personInfo = ("Diana", 32, "New York")
print(personInfo[0])
print(personInfo[1])

Tuples also facilitate simultaneous assignment of multiple variables:

name, age, country, career = ('Diana', 32, 'Canada', 'CompSci')
print(country)

The tuple’s values are on the right, while the variables receiving those values are on the left.

Modifying Tuples

Although you can’t alter the content of a tuple, you can concatenate or join two tuples using the + operator:

x = (3,4,5,6)
x = x + (1,2,3)
print(x)

Enhance your Python skills with these Downloadable Python Exercises.

← Previous Topic | Next Topic →






Leave a Reply:




Lee Sun, 07 Jun 2015

Why would you use a Tuple over a List? What's the difference?

Frank Sun, 07 Jun 2015

Tuple is another datastructure, it has different properties. A tuple has no methods and cannot be changed once created. Tuples may be faster than lists to iterate.

Leasinop Sun, 07 Jun 2015

I also read that tuples are heterogeneous (could be used for various data types) while lists are homogeneous (should be used for one data type)

Pradeep Kaja Fri, 19 Jun 2015

I do not use this wording of Heterogeneous/Homogeneous. Its very confusion, because we can insert values of different data types in both list and tuple. I believe the only difference is that List is mutable Tuple is not. Tuple is fast accessed on memory when compared to List in some cases.

Reddy Fri, 26 Jun 2015

list are homogeneous.. ??? l= ["a","b","c"]; l.append("d"); l.append(123); I added a string type and an integer type. Both worked fine.

Frank Fri, 26 Jun 2015

Hi Reddy, a python list can hold multiple datatypes:

#!/usr/bin/env python

l= ["a","b","c"];
l.append("d");
l.append(123);

print(l)

# print datatype
for item in l:
print(type(item))

Output:

['a', 'b', 'c', 'd', 123]
<class 'str'="">
<class 'str'="">
<class 'str'="">
<class 'str'="">
<class 'int'="">
Lim Thu, 20 Aug 2015

why we have to use tuple? it seems all tuple's usage also can be achieved by list.

Frank Thu, 20 Aug 2015

Lists are dynamic in size while tuples are fixed. You can't add or remove elements to a tuple. Tuples are faster than lists. They secure from data being modified.

Lei Wed, 25 Nov 2015

person = ('Diana','Canada','CompSci')
s = ''.join(person)
print(s)

i can,t understand

Frank Thu, 26 Nov 2015

First line is a tuple (pair of variables). Then a string is created and printed.

Vaishnavi Tue, 08 Dec 2015

How does Join work ?
When I give -

   person = ('Diana','Canada','CompSci' )
s = '-trial '.join(person)
print(s)

, the output is " Diana-trial Canada-trial CompSci" . The join does not happen for the last value.

Frank Tue, 08 Dec 2015

Hi vaishnavi, I'll look at this asap.

Frank Thu, 10 Dec 2015

str.join(iterable)
Join returns a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.
In this case we have concatenations (Diana, Canada) and(Canada, CompSci). They are connected with the separator.

To add the text '-trial' to each element, simply use a loop on the list with a concatenation.

Vaishnavi Thu, 07 Jan 2016

Hi Frank, thanks a lot for your reply..!
[ Sorry for the late reply ..]
I understood why it is not added at the end. But how do we achieve this using a loop.
Is the below code correct ?

person = ('Diana', 'Canada', 'CompSci' )
for i in range (len(person)):
s = '-trial'.join(person)
print (s)


Output: Diana-trialCanada-trialCompSci
Even here the concatenation does not happen for the last one .

Frank Fri, 08 Jan 2016

You could do this:

s = ""
person = ('Diana', 'Canada', 'CompSci' )
for i in range (len(person)):
s = s + person[i] + "-trial "
print (s)


which outputs:
Diana-trial Canada-trial CompSci-trial

Vaishnavi Sat, 09 Jan 2016

Thanks Frank, it works.
Also thanks for such a wonderful tutorial !!

Reeba Thu, 28 Jan 2016

but we can Append to a tuple in Python,which will change its size.so how can we say that its size is fixed

Frank Thu, 28 Jan 2016

Hi Reeba, technically we are not appending but creating a new tuple that replaces the old one. In the example above we say "x = " which recreates a new tuple. In practice this is of course appending, but technically on the computational level we are replacing the memory.

Reeba Tue, 02 Feb 2016

Thanks Frank !!!