Newest Visitor Badge Earned!
Click to create an account and start competing today!
After Hours Programming
Show Tutorials


You (Level 0)
0/25
Last Badge Earned
Newest Visitor

Python Tuples Tutorial

Python Tuples

The Python Tuples tutorial explains what tuples are and how to use them in Python.

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.

Try testing your code with the code simulator!

Let's move on to some more tutorials or the next section!



Previous Tutorial

Lists

Next Tutorial

Dictionaries







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

Stumbleupon Facebook Twitter GooglePlus Reddit Delicious Digg


Comment or Suggestion?

Comments are a way to tell me about corrections or suggestions, what you thought about the resource, or providing additional information. Help feed the discussion!


Yes




Comments on Python Tuples

user
Anoubys
175
June 10, 2014 10:37AM
this way you convert it back:

myTuple = tuple(myList)


user
Malthus101
100
May 31, 2014 11:57AM
Interesting lesson which I understand but as someone else pointed out, the .append command IS appending the tuple.


user
Haoji Wang
175
May 26, 2014 10:49AM
Hi there, just found an error, when i append number in tuple, it works on the online code simulator.


user
Vicki
400
April 20, 2014 10:24AM
Okay, so you can change a tuple into a list for modification, but can you change it back to a tuple?


user
panda
100
March 17, 2014 05:16AM
This is helpful


user
Lotfi GHAZOUANI
200
February 26, 2014 03:57PM
Important difference between Lists and Tuples in Python


user
Northoran
100
February 17, 2014 08:49PM
Great to learn.


user
novicepro
200
January 2, 2014 12:22AM
never mind...parentheses vs. brackets very important distinction!!


user
novicepro
200
January 2, 2014 12:06AM
Need help!!! my Tuple is changing with append command :/


user
Hjelmdall
100
October 11, 2013 11:48PM
Mention that tuples use "()" where lists use "[]"


user
Chris
September 18, 2013 11:31AM
Hang on, you haven't casted the myTuple into a list, you've created a NEW list from the original tuple.
Would this not better show re-casting?
myTuple = (1,2,3)
myTuple = list(myTuple)
''' test this by type(myTuple) which returns a list type'''
myTuple.append(4)
myTuple = tuple(myTuple) '''to switch it back to a tuple'''


user
Rebarakaz
250
September 14, 2013 04:37AM
Man, you rock..!!
I started to love this thing :)
I thought Python was so difficult to learn..
Thanks mate..


user
James Fardeen
175
June 25, 2013 01:04PM
Yea it worked

thanks man


user
Hamoud
June 9, 2013 07:10AM
SachinA2P	...

I tried it and I got it correct:

myTuple2 = tuple(myList2)
print (myTuple2)


user
SachinA2P
100
June 7, 2013 02:19AM
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"


user
Kevin
April 14, 2013 12:24PM
Great tutorial so far. I've tried to learn Python a few times before but have quit due to lack of time to finish a tutorial. I like how you just teach the basics of python which will allow me to experiment on my own while having a basic understanding of how the language works.