Home » Tutorials » Python » Tuples

Tuples

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]
myList.append(4)
print (myList)

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

myTuple2 = (1,2,3)
myTuple2.append(4)
print (myTuple2)
Result [1, 2, 3, 4]
(1, 2, 3)
Traceback (most recent call last):
File “C:/Python32/test”, line 9, in
myTuple2.append(4)
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)
myList.append(4)
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.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Tuples". After Hours Programming. Accessed on September 19, 2024. https://www.afterhoursprogramming.com/tutorial/python/tuples/.

  • Stewart, Suzy. "Tuples". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/python/tuples/. Accessed 19 September, 2024.

  • Stewart, Suzy. Tuples. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/python/tuples/.



18 thoughts on “Tuples”

  1. Now days in the OO programing everything tends to be an object. An object with properties and methods. List are object too, and Tuples are the same objects with the lack of some List methods, like append. If it doesn’t have it you can’t call it, that simple, that is why you cannot append a value to a Tuple. Unless like in other languages you could change its prototype, this is its base class, so you could create a Tuple with that method or a new class with that and other methods you might prefer or need.

  2. 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"

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.