Skip to Content

You (Level 0)
0% Complete
Last Badge Earned

In Python, tuples are almost identical to lists. So, why should we use them you ask? The one main difference between tuples and lists are that tuples cannot be changed. That is to say you cannot add, change, or delete elements from the tuple. Tuples might seem odd at first, but there is a great reason behind them being immutable. As programmers, we mess up occasionally. We change variables that we didn't want to change, and sometimes, well, we just want things to be constant so we don't accidentally change them later. However, if we change our minds we can also convert tuples into lists or lists into tuples. The fact is we need to make the conscious effort to say Python, I want to change this tuple into a list so I can modify it. Enough babbling, let's see a tuple in action!

List vs. Tuple

Example myList = [1,2,3]
print (myList)

myTuple = (1,2,3)
print (myTuple)

myTuple2 = (1,2,3)
print (myTuple2)
Result [1, 2, 3, 4]
(1, 2, 3)
Traceback (most recent call last):
File "C:/Python32/test", line 9, in
AttributeError: 'tuple' object has no attribute 'append'

So, we see that the list clearly works as expected. We just append a 4 onto the end, and Python doesn't miss a beat. Next, we test out our tuple declaration and it works as well. But when we try to append to the tuple, Python give us a nasty little error. Like I said, you cannot change a tuple! Python will bite you if you try using things like append on a tuple. But, let's say "Hey, I really want to add a four to that tuple." Let's do it:

Example myTuple = (1,2,3)
myList = list(myTuple)
print (myList)
Result [1, 2, 3, 4]

Boom! We have successfully undone what Python was trying to teach us not to do. We just casted the tuple into a list, then once it was a list, we used it's append method to add the 4. It should be reiterated that the purpose of a tuple is to be immutable. If you are planning to change the variable, just use a list instead.

Your code will execute in this window.

Let's explore some more tutorials or topics!

Previous Tutorial Next Tutorial

If you enjoyed this resource, support me by sharing this page with others.

Comment on

  • User

    Rather Not Nov. 5, 2014, 12:10 p.m.

    Small punctuation error in this line: "we used it's append method to add the 4" It should be "its" instead of "it's".

  • User

    Eleanor Oct. 22, 2014, 1:42 p.m.

    i love these tutorials

  • User

    Anoubys June 10, 2014, 10:37 a.m.

    this way you convert it back: myTuple = tuple(myList)

  • User

    Malthus101 May 31, 2014, 11:57 a.m.

    Interesting lesson which I understand but as someone else pointed out, the .append command IS appending the tuple.

  • User

    Haoji Wang May 26, 2014, 10:49 a.m.

    Hi there, just found an error, when i append number in tuple, it works on the online code simulator.

  • User

    Vicki April 20, 2014, 10:24 a.m.

    Okay, so you can change a tuple into a list for modification, but can you change it back to a tuple?

  • User

    panda March 17, 2014, 5:16 a.m.

    This is helpful

  • User

    Lotfi GHAZOUANI Feb. 26, 2014, 3:57 p.m.

    Important difference between Lists and Tuples in Python

  • User

    Northoran Feb. 17, 2014, 8:49 p.m.

    Great to learn.

  • User

    novicepro Jan. 2, 2014, 12:22 a.m.

    never mind...parentheses vs. brackets very important distinction!!

  • User

    novicepro Jan. 2, 2014, 12:06 a.m.

    Need help!!! my Tuple is changing with append command :/

  • User

    Hjelmdall Oct. 11, 2013, 11:48 p.m.

    Mention that tuples use "()" where lists use "[]"

  • User

    Rebarakaz Sept. 14, 2013, 4:37 a.m.

    Man, you rock..!! I started to love this thing :) I thought Python was so difficult to learn.. Thanks mate..

  • User

    SachinA2P June 7, 2013, 2:19 a.m.

    How to convert list to touple? As mentioned "However, if we change our minds we can also convert tuples into lists or lists into tuples". But when I tried: my_touple = touple(my_otrlst) print my_touple it threw "NameError: name 'touple' is not defined"