Lists: keeping many items together — Computing, 7–10
A list lets a program keep a group of related things in one place, such as names, scores or shopping items. You can add, remove and find things without making a new box for every item.
One place for a group of things
Imagine a shopping list on paper: apples, milk and rice have one heading, but each item is still separate. A computer list works in much the same way. It gives the program one name for the group, while keeping the items in an order that can be changed.
Why make a list?
Without a list, a program that stores ten scores might need ten separately named boxes, and twenty scores would need twenty. That becomes awkward to read and change. Lists solve this problem by letting one set of instructions handle a group, even when its size changes.
A list of four scores
Mina records the scores 6, 8, 5 and 9. First, she puts them in one list: [6, 8, 5, 9]. Next, the program adds 7, so the list becomes [6, 8, 5, 9, 7]. To find the highest score, it checks the items and gets 9; to remove 5, it takes that item out.
A list is not one giant thing
It is easy to think that a list has one value, because it has one name. In fact, each item keeps its own value and position. If a program changes the third item, it changes 5 in [6, 8, 5, 9], not the whole list. This matters when you read or replace one item.
Lists outside a lesson
Music apps use lists for songs in a queue, games use them for a player's items, and shops use them for products in a basket. A phone's contacts are also a group of records that can grow, shrink or be searched. The same idea works whenever many similar things need managing.
Keep exploring
Other languages
Loading MyLeoNes™…