What is the difference between a list and a tuple?
I think that a list is a set of elements that can be numbers or strings, or a subset. a tuple is a sequence of instructions.
One functional aspect is that a list is mutable and a tuple is not.
The principal difference is this: a tuple is very much like a list except that it is immutable (i.e. it can't be changed). Both are object types and can be used for grouping data into a single object. Before looking at how they are different let's look at how they are similar: --> Like lists, tuples are *ordered* collections of objects --> Like lists, tuples can embed any kind of object --> Like lists, tuples are accessed by offset and, therefore, support offset-based access operations such as indexing and slicing. If you wanted to use an object that consists of the first 5 prime numbers, you can use either a list or a tuple: a = [2, 3, 5, 7, 11] b = (2,3,5,7,11) Thus, print a[2] print b[2] will give you the same result, namely the number 5 But suppose you want to change a and b to add the next prime number. The following works perfectly for a, which is a list and is *mutable* a.append(13) a has now mutated (i.e. changed) to the object: [2,3,5,7,11,13] Try to change or mutate a tuple and you will get an error message. In fact, you can't perform any operations on tuples which change the object once defined. Why might you use tuples? When you have a set of objects that you are fixed and you don't expect to change throughout the life of your program. For example, if you want to store the letters in the English alphabet. ('a', 'b',....)
given all that, lists have lots of methods and sometimes it makes things easier to turn you tuple into a list then do stuff to it then turn it back into a tuple
Join our real-time social learning platform and learn together with your friends!