2

Here I'm not talking about a particular programming language. I would like to know what is the difference between these things as between different data structures. I assume lists are basically supposed to be resizable whereas arrays are not, but I'm not sure.

Kaiyaha
  • 448
  • 3
  • 9

2 Answers2

3

Talking about "arrays" and "lists" as abstract data types without referring to any particular implementation can be a bit confusing, because those two are not very well defined.

The two Wikipedia pages List (abstract data type) and Array data type make this ambiguity somewhat evident, with uncertain statements such as "Implementation of the list data structure may provide some of the following operations:".

In many languages, there is a type called list and a type called array and they have some better defined meaning.

Here is a very general summary.

Lists:

  • a list is linear, it is an ordered sequence of elements;
  • you can access the first element of a list;
  • you can add a new element to a list;
  • you can access all elements of the list one after the other, starting from the first element. That last operation is done either with "arbitrary access" (accessing elements as l[0], l[1], l[2], etc.) or with two operations called "head" and "tail", where head(l) returns the first element of l and tail(l) returns the sublist obtained by discarding the first element.

In my mind, a list of four elements looks like this:

-> 3 -> 7 -> 12 -> 9

Arrays:

  • an array provides "arbitrary access" using indices (accessing elements as a[something]);
  • sometimes the index is restricted to integers between 0 and the length of the array; sometimes the index can be anything and everything;
  • you can easily modify elements that you access, but you cannot necessarily add new elements.

An array which allows anything to be used as index is usually called a map or a dict in most languages, where array refers strictly to linear structures indexed by integers; but in a few languages, for instance PHP, it is still called an array.

In my mind, an array of four elements looks like this:

+--+--+--+--+
| 0| 1| 2| 3|
+--+--+--+--+
| 3| 7|12| 9|
+--+--+--+--+

Tuples:

  • a linear, ordered sequence of elements;
  • usually can contain elements of different types, whereas in most languages, all the elements in a list must have the same type;
  • usually cannot add/remove elements
  • in strongly-typed languages, such as Haskell or OCaml, the type of a tuple is given by its length and the enumeration of the types used in it, for instance the tuple (3, 7, 12.0, "9") has type (int, int, float, string), and a function returning a specific type of tuple cannot suddenly return a tuple of a different type.

Tuples in strongly-typed languages are sometimes called "product types" by analogy with the Cartesian product in mathematics, and are very close to struct in C. Tuples in weakly-typed languages are very close to lists.

Stef
  • 13,242
  • 2
  • 17
  • 28
  • Thanks, sounds reasonable! What about tuples? Wiki states it is also an ordered sequence of elements – Kaiyaha Nov 02 '20 at 09:55
  • 1
    I added a small section for tuples. – Stef Nov 02 '20 at 11:20
  • well in `Python` for example `lists` are heterogeneous – Kaiyaha Nov 02 '20 at 11:22
  • 1
    Yes; in strongly-typed languages, lists can vary in length but must contain elements of the same type, and tuples are used when a known number of elements of different types must be held together; in this case, there is not much difference between a tuple (int, float, string) and three variables int, float and string, except the convenience of having one "tuple" variable holding the three subvariables. In weakly-typed languages, lists can store elements of different types and tuples are not very different from lists. – Stef Nov 02 '20 at 11:25
0

Yeah, you are right.

Array can be created with fixed size and it is not possible to add items if array is full.

List is dynamic array and it can add items how many you want. Under the hood array is used in List in some languages, e.g. in C#. When new item is added and an array is full, then new array will be created with doubled size.

You can see the implementation of List<T> in C#

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • What about tuples then? When I try to find out how a tuple differs from a list and an array, all I see is Python. Could you give a brief explanation how tuples differ from lists and arrays? – Kaiyaha Nov 02 '20 at 09:00
  • @Kaiyaha there is a great answer about your question [C# Tuple versus List Considerations](https://stackoverflow.com/questions/18591253/c-sharp-tuple-versus-list-considerations) and [Nice post about Tuples vs Class](https://stackoverflow.com/questions/44650636/when-to-use-tuple-vs-class-c-sharp-7-0) – StepUp Nov 02 '20 at 09:09
  • I mean tuples as a data structure in structure theory (kind of), no tuples in a particular language. Wiki makes lists and tuples looking identical (both are ordered sequences) – Kaiyaha Nov 02 '20 at 09:11
  • @Kaiyaha could you give me a link to tuple and links as data structure? – StepUp Nov 02 '20 at 09:13
  • https://en.wikipedia.org/wiki/List_(abstract_data_type) https://en.wikipedia.org/wiki/Tuple – Kaiyaha Nov 02 '20 at 09:14
  • @Kaiyaha it looks like `tuple` here means like set of items – StepUp Nov 02 '20 at 09:19
  • well how a tuple differs from a set then, really confusing – Kaiyaha Nov 02 '20 at 09:21
  • "Under the hood array is used in List" That is true for some implementations of lists in some programming languages, and far from universal. – Stef Nov 02 '20 at 09:25
  • https://www.reddit.com/r/learnprogramming/comments/3ha92l/what_is_the_difference_between_a_tuple_list_and/ Found this, what do you think? – Kaiyaha Nov 02 '20 at 09:26
  • @Stef thanks, I've edited my reply. Could you tell in what languages is far from universal? – StepUp Nov 02 '20 at 10:52
  • @Kaiyaha do you mean `list` as `linkedlist`? – StepUp Nov 02 '20 at 10:55
  • @StepUp no, I consider `linkedlist` to be one of the varieties of `list` – Kaiyaha Nov 02 '20 at 10:56
  • 1
    @StepUp For instance, Haskell and OCaml both use singly linked lists; Java allows the user to choose explicitly between ArrayList and LinkedList; even C++'s `std::list` ["is usually implemented as a doubly-linked list"](https://en.cppreference.com/w/cpp/container/list). – Stef Nov 02 '20 at 11:07