Skip to content

Programming Basics

Lists/Arrays

The idea, in plain English

A list, also called an 'array', is a numbered row of lockers, all lined up. Each locker holds one item. You find something by its locker number, called an 'index'. The very first locker is always number 0, not 1.

How it works

  1. 1Create a list by putting items between square brackets, separated by commas: `["apple", "banana"]`.
  2. 2Read or change an item using its index in square brackets, like `fruits[0]` for the first one.
  3. 3Add items to the end, remove them, or ask for the list's length — how many lockers are filled.

When you'd use it

Use a list any time you have a bunch of similar things to keep together and work with as a group — a shopping list, a set of scores, or all the comments on a post.

Common beginner mistakes

  • Assuming the first item is at index 1. It's actually index 0, which trips up almost everyone at first.
  • Asking for an index that doesn't exist, like the 10th item in a 3-item list. This crashes the program or gives an error.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Fruits: apple, banana, cherry, date
First fruit: apple
Number of fruits: 4

Not sure this is the right topic? See the learning paths → or where this leads →