Python lists and tuples seem to be no different for a beginner except that tuples are immutable and lists are mutable. However, what I have just mentioned is the Technical difference. But there is a Cultural difference aspect of this discussion. Figuring out the Cultural difference allows us to answer the following questions: When do I use tuples? When do I use lists?
First, let us take a look into the similarities between lists and tuples:
Now, let us take a look at the Cultural difference between lists and tuples. Lists are used when you have elements of a similar type of unknown length. Tuples are used when you have elements of different types of known length.
For example:
not know the exact number of students.
["Tim", "Nitin", "Bob", "Julian", "Mike”] # The elements are of same type(string)
("Tim", 10, "6", "DPS", True) # The elements are of different types.
In this aspect, tuples represent a database record.
Keep in mind the Technical difference between lists and tuples. However, lists are generally used when you have homogenous items of unknown length, and tuples are used when you have heterogeneous items of known length.
Leave a comment