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