What is the difference between a tuple and a list?
Tuples vs Lists, when do you decide which one you use?
python - What's the difference between lists and tuples? - Stack Overflow
tuples vs lists
What is an example of a tuple?
What is the biggest difference between a tuple and a list?
Why is a tuple faster than a list?
Ive gotten through chapter 4 of Python crash course and dont know what the big difference between the two are.
Hey r/learnpython, I have noticed I often default to lists in the expectation I will need them to be mutable but not sure what the common practice is.
If you are unsure if you will need it to be mutable, do you start with a tuple just for efficiency's sake and go back and make it a list if you find later you need it to be mutable?
Or start with a list and make it a tuple if you later realize you never needed to actually change it?
What is the common practice here? I think I am underusing tuples and overusing lists.
Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.
Using this distinction makes code more explicit and understandable.
One example would be pairs of page and line number to reference locations in a book, e.g.:
my_location = (42, 11) # page number, line number
You can then use this as a key in a dictionary to store notes on locations. A list on the other hand could be used to store multiple locations. Naturally one might want to add or remove locations from the list, so it makes sense that lists are mutable. On the other hand it doesn't make sense to add or remove items from an existing location - hence tuples are immutable.
There might be situations where you want to change items within an existing location tuple, for example when iterating through the lines of a page. But tuple immutability forces you to create a new location tuple for each new value. This seems inconvenient on the face of it, but using immutable data like this is a cornerstone of value types and functional programming techniques, which can have substantial advantages.
There are some interesting articles on this issue, e.g. "Python Tuples are Not Just Constant Lists" or "Understanding tuples vs. lists in Python". The official Python documentation also mentions this
"Tuples are immutable, and usually contain an heterogeneous sequence ...".
In a statically typed language like Haskell the values in a tuple generally have different types and the length of the tuple must be fixed. In a list the values all have the same type and the length is not fixed. So the difference is very obvious.
Finally there is the namedtuple in Python, which makes sense because a tuple is already supposed to have structure. This underlines the idea that tuples are a light-weight alternative to classes and instances.
Difference between list and tuple
Literal
someTuple = (1,2) someList = [1,2]Size
a = tuple(range(1000)) b = list(range(1000)) a.__sizeof__() # 8024 b.__sizeof__() # 9088Due to the smaller size of a tuple operation, it becomes a bit faster, but not that much to mention about until you have a huge number of elements.
Permitted operations
b = [1,2] b[0] = 3 # [3, 2] a = (1,2) a[0] = 3 # ErrorThat also means that you can't delete an element or sort a tuple. However, you could add a new element to both list and tuple with the only difference that since the tuple is immutable, you are not really adding an element but you are creating a new tuple, so the id of will change
a = (1,2) b = [1,2] id(a) # 140230916716520 id(b) # 748527696 a += (3,) # (1, 2, 3) b += [3] # [1, 2, 3] id(a) # 140230916878160 id(b) # 748527696Usage
As a list is mutable, it can't be used as a key in a dictionary, whereas a tuple can be used.
a = (1,2) b = [1,2] c = {a: 1} # OK c = {b: 1} # Error
So below i was wondering what the difference is in using a tuple vs a list in a dictionary below is the code. IK that lists are easier to modify and that tuples are immutable but like how does that actually come into effect in code. If someone could give an example that would be great. So ig you would use a list if you wanted to append or change the data?
course = { "language": ["French", "German"], "duration": "3 months" } print(course)
vs
course = { "language": ("French", "German"), "duration": "3 months" } print(course)