Thursday, July 5, 2018

What are the differences between a list and a tuple in Python?

You have seen what a list is. Tuple is a standard data type for sequence and it is immutable.

According to Python site:

"Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
"
The following table prepared using:
Python 3.7.0b5 (v3.7.0b5:abb8802389, May 31 2018, 01:54:01) [MSC v.1913 64 bit (AMD64)] on win32

shows the differences. There is little you can do with a tuple because of its immutablity compared to Lists. Of course define them correctly.


No comments: