Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

What's the difference?

What are the advantages/disadvantages of tuples/lists?

1 Answer

0 votes
by (106k points)

When we talk about the difference between list and tuple so the main difference is lists are mutable(vales of lists can be changed after assignment) whereas tuples are immutable(once values are assigned to tuple they can not be changed).

As we have known that 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 ordered.

Another example would be pairs of the page and line numbers to reference locations in a book.

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.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 1, 2019 in Python by Sammy (47.6k points)
+2 votes
3 answers
0 votes
1 answer

Browse Categories