Python tuple

The tuple data structure is used to store a group of data.  The elements in this group are separated by a comma. Once created, the values of a tuple cannot change.  

Video: Tuples Get PDF Get Exercises Quiz

Python Tuple

An empty tuple in Python would be defined as:

tuple = ()

A comma is required for a tuple with one item:

tuple = (3,)

The comma for one item may be counter intuitive,  but without the comma for a single item, you cannot access the element.  For multiple items, you do not have to put a comma at the end.  This set is an example:

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

The data inside a tuple can be of one or more data types such as text and numbers.

Data access

To access the data we can simply use an index. As usual, an index is a number between brackets:

#!/usr/bin/env python 
personInfo = ("Diana", 32, "New York")

If you want to assign multiple variables at once, you can use tuples:

#!/usr/bin/env python
name,age,country,career = ('Diana',32,'Canada','CompSci')

On the right side the tuple is written. Left of the operator  equality operator are the corresponding output variables.

Append to a tuple in Python

If you have an existing tuple, you can append to it with the + operator.  You can only append a tuple to an existing tuple.

#!/usr/bin/env python
x = (3,4,5,6)
x = x + (1,2,3)

19 thoughts on “Python tuple

  1. Vaishnavi - December 8, 2015

    How does Join work ?
    When I give –
    person = (‘Diana’,’Canada’,’CompSci’ )
    s = ‘-trial ‘.join(person)
    , the output is ” Diana-trial Canada-trial CompSci” . The join does not happen for the last value.

    1. Frank - December 8, 2015

      Hi vaishnavi, I’ll look at this asap.

    2. Frank - December 10, 2015

      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.

      1. Vaishnavi - January 7, 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 .

        1. Frank - January 8, 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

          1. Vaishnavi - January 9, 2016

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

  2. Lei - November 25, 2015

    person = (‘Diana’,’Canada’,’CompSci’)
    s = ”.join(person)

    i can,t understand

    1. Frank - November 26, 2015

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

  3. Lim - August 20, 2015

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

    1. Frank - August 20, 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.

      1. Reeba - January 28, 2016

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

        1. Frank - January 28, 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.

          1. Reeba - February 2, 2016

            Thanks Frank !!!

  4. Pradeep Kaja - June 19, 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.

  5. Lee - June 7, 2015

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

    1. Frank - June 7, 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.

    2. Leasinop - June 7, 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)

      1. Reddy - June 26, 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.

        1. Frank - June 26, 2015

          Hi Reddy, a python list can hold multiple datatypes:

          #!/usr/bin/env python
          l= ["a","b","c"];
          # print datatype
          for item in l:


          ['a', 'b', 'c', 'd', 123]
          <class 'str'>
          <class 'str'>
          <class 'str'>
          <class 'str'>
          <class 'int'>