When teaching students Python lists (which are more like arrays, not linked-lists) I'd like to use metaphor of list being a collection of boxes put in row.
But this doesn't work well:
- indexes are important to be numbers, but in my example indexes should actually be counted, and it is not obvious that "counted" and "index" are same. At least, from performance PoV indexes are not "counted"
- and I can't name boxes with increasing numbers, as this is plain wrong, indexes are not names of boxes, they are property of list itself. For example, list slice or splice should change names of boxes, which isn't realistic
- the boxes metaphor fails for list of lists
A slightly better metaphor for list of integers is:
- list is a train with wagons of same length
- and indexes are special signs on railway platform
- the train stays near platform in such way, that the connection between train and first wagon is next to sign with number "0", index 0
- other signs on platform stays near other connections in an increasing order
- the train head and railway platform together are a list, the wagons are elements of a list
- any slice or splice of train wagons is done with train head staying where it stays and extending/squashing all remaining wagons to be connected.
This is slightly more correct, but fails for list of lists (or list of objects). Are there better metaphors?
list[i]
is just a shortcut forarray_offset + i * box_size
. Splice operations do move contents to different boxes/addresses. Your train metaphor suggests sequential memory access, but RAM allows random access. Your metaphor only struggles with lists of lists because you suggest that the waggons contain the object rather than a pointer to the object. Once you've established basic intuition, telling the truth might be simpler. – amon Oct 18 '20 at 05:57list
is an abstract data type which is precisely defined by the operations which are implemented in the specific language or framework - which metaphors won't tell. A concept like lists is best explained by showing students some problems which can be solved by them, and even more by letting them solve some problems on their own. – Doc Brown Oct 18 '20 at 07:10