On the crucial differences between tuples and lists in Python
Listing is sort of like tupling, except lists are more loosey goosey. The stuff in a tuple is more solid than rock. It can’t be changed, not with a chisel, a jackhammer or bunker-busting bomb (however, see the postscript below for a loophole).
Lists, in contrast, are more flexible than your average yogi. The data in a list can be changed, and the lists themselves can grow longer or shorter at will (or close to it).
You recognize a list because it comes in brackets rather than parentheses. So, here’s an example of a list:
thisList = [“Joe”, “Shiho”, “Jose”]
What’s cool about the list is that we can swap elements in or out. Let’s say Joe is just getting too gross for the poker-playing gang, with his jalapeno nacho breath and his cheesy fingers. We dump Joe from the list and substitute in Sarah, who makes an awesome bean dip. She’s on the list now, and Joe is out.
Here’s how you could do that in Python:
thisList = ["Joe", "Shiho", "Jose"] thisList.remove("Joe") thisList.append("Sarah") print (thisList)
That will print your new list, the one with Joe gone and Sarah added::
["Shiho", "Jose", "Sarah"]
If you try to do the same thing with a tuple, it won’t work. Therefore, if you need some flexibility, go with the list. If you need tight, anal-compulsive security, go with the tuple.
PS: If you want some more information on lists, I recommend TutorialsPoint.
PPS: Some experts have noted that tuples are immutable but not necessarily unchanging. Yeah, I know that sounds like a contradiction. To learn about it, though, have a look at “Python tuples: immutable but potentially changing”.
Features image: Brazilian poker player Maria Eduarda "Maridu" Mayrinck during the World Series of Poker, 2009, Author: Juliano Maesano. From https://commons.wikimedia.org/wiki/File:Maridu.jpg