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
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:
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.
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"
Yea it worked
thanks man
Man, you rock..!!
I started to love this thing 🙂
I thought Python was so difficult to learn..
Thanks mate..
Mention that tuples use "()" where lists use "[]"
Need help!!! my Tuple is changing with append command :/
never mind…parentheses vs. brackets very important distinction!!
Great to learn.
Important difference between Lists and Tuples in Python
This is helpful
Okay, so you can change a tuple into a list for modification, but can you change it back to a tuple?
Hi there, just found an error, when i append number in tuple, it works on the online code simulator.
Interesting lesson which I understand but as someone else pointed out, the .append command IS appending the tuple.
this way you convert it back:
myTuple = tuple(myList)
Small punctuation error in this line:
“we used it’s append method to add the 4”
It should be “its” instead of “it’s”.
Simple explanation and very easy to understand.I am really enjoying your tutorials..Thank you..
why the difference in parentheses and brackets ?
why the difference in parentheses and brackets ?
and: tuple and list.
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.
you can do pattern matching on tuple like this:
def getChild():
return ( “Jake”, 4)
(name,age) = getChild()
print(name)
print(age)