0

As far as I know, an array is an unresizable indexed sequence, usually with homogeneous data elements, whereas a list is a resizable ordered sequence supporting heterogeneous data. Wiki states that a tuple as a structure or a math model is a finite ordered list (sequence) of elements. How does it differ from a list or an array?

Note! This question is not about structure implementation in any programming language. I'm only curious about pure data structures, i.e. by definition.

Kaiyaha
  • 448
  • 3
  • 9

2 Answers2

1

One main difference between lists and tuples in python is that tuples are immutable (i.e the values stored within them cannot be changed), whereas lists and arrays are mutable. This can be demonstrated with this piece of code:

tup = ((1,2),(3,4))
tup[0][0] = 3

Output:

TypeError: 'tuple' object does not support item assignment

But when the same is done with a list:

lst = [[1,2],[3,4]]
lst[0][0] = 3

It does not produce any error.

This is the main difference between lists and tuples, but there are many other differences as well, such as performance and memory consumption. You can refer this website to know more.

Sushil
  • 5,440
  • 1
  • 8
  • 26
  • Thanks, but again, I do not imply language implementation – Kaiyaha Nov 02 '20 at 10:23
  • Ok...Then what r u exactly looking for? – Sushil Nov 02 '20 at 10:27
  • you explaining a `Python` implementation, there is nothing new to me here. If you do want to bind my question with a particular implementation, assume I'm asking about common features in all programming language implementations. I am asking about a model of a tuple in computer science, not how it works in Python – Kaiyaha Nov 02 '20 at 10:30
  • Ok...According to my knowledge, tuples are immutable in all programming languages. – Sushil Nov 02 '20 at 10:32
  • 1
    I think they are, not sure – Kaiyaha Nov 02 '20 at 10:33
1

I think the main difference that separates arrays from tuples or lists is contiguity in memory and homogeneity of its constituent elements. From computer science perspective, an array is basically a contiguous chunk of memory storing objects of same nature, whereas list and tuple can have their elements in distant pockets in memory and also of varying nature. Also, tuple enforces an ordering of its element (think of cartesian coordinates (x, y)) while list is basically an unordered multiset kind of thing!

As an example you cannot interchange x and y field of a coordinate. But if a region in plane is represented by a bunch of points, you don't care about the order of the points. Depending on the ease of traversing or searching in the bunch of points, you can use different data structures like list, set or even tuple (say the order is important, like clockwise traversal of vertices of a polygon)!

swag2198
  • 2,546
  • 1
  • 7
  • 18
  • Actually list is ordered by definition – Kaiyaha Nov 02 '20 at 10:40
  • May be, but you have to define on what property the `order` of the elements in a list is based on. Otherwise, we simply say it as an `unordered list` or `list`. An `ordered list`'s example may be a sorted one (magnitude ordering property). – swag2198 Nov 02 '20 at 10:43
  • 1
    [Check this out](https://en.wikipedia.org/wiki/List_(abstract_data_type)). It's not quite about how list elements are placed in memory physically – Kaiyaha Nov 02 '20 at 10:47
  • Yeah so basically a container containing finite number of ordered elements is a `list`. I guess here the `order` is enforced by the way the elements were appended to create the `list` initially. But still a list is not a `random access` data structure like an (indexed) array, where the indexing (and hence random access of any element) comes strongly from contiguity in memory. – swag2198 Nov 02 '20 at 10:54
  • 1
    [This answer](https://stackoverflow.com/a/64643051/13935289) states a `list` can be a `random access` structure. The main problem here I think is that all this structures are not well defined as pure structures, which produce confusement – Kaiyaha Nov 02 '20 at 11:00
  • 1
    Interesting! I think the practical uses of these elementary structures are so profound that everyone ignores their seemingly loose definitions! Still I might be wrong, a more abstract math oriented person can have a better say! – swag2198 Nov 02 '20 at 11:04
  • I suggest you ask this in https://math.stackexchange.com/ or https://cs.stackexchange.com/. You can have a clearer view if any! – swag2198 Nov 02 '20 at 11:06
  • 1
    [Another brief explanation](https://computersciencewiki.org/index.php/Lists) if you wonder. To me seems like I have found out enough – Kaiyaha Nov 02 '20 at 11:11