-1

It seems to me that the only difference between a tuple and a list is that a tuple is unchangable. If this is true, defining a new type of lists by requiring them to be unchangable seems to be more intuitive than bothering to introduce a new name (tuple) and new syntax (round-brackets). Is tuple important enough for a new name and syntax?

Youjun Hu
  • 991
  • 6
  • 18
  • 2
    The main difference is what you are signaling to the reader of your code that this data structure should do. A list is changble, dynamic in size, and should contain homogenous data. A tuple is general short, contains heterogenous data (in the same order, i.e. (str, int, bool)* and represent as sort of *record*. They are more compact than lists, and the order matters. – juanpa.arrivillaga Jun 25 '20 at 06:51
  • 2
    Note, the syntax isn't round-brackets. *commas* make the tuple. e.g. `a_tuple = 'foo', 42` – juanpa.arrivillaga Jun 25 '20 at 06:52
  • 1
    I do not think this question is opinion-based. I am just asking the reason behind a data structure design. – Youjun Hu Jun 25 '20 at 06:54
  • 1
    See this duplicate: https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples – juanpa.arrivillaga Jun 25 '20 at 06:54
  • @juanpa.arrivillage Except of the empty one, which works without. – Klaus D. Jun 25 '20 at 06:55

1 Answers1

1
  • Iterating through a tuple is faster than compared to a list (by a small amount)
  • Tuples that contain immutable elements can be used as a key for a dictionary, list is not hashable

There can be many more reasons like passing data that you don't want any code to change but these are the major ones

Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22