Array

An array is a collection of stored data that can hold values of various data type. Arrays themselves are considered to be a composite data type. Python uses lists as an equalivalent type.

Syntax

Arrays are often created by using square brackets [] with a comma-separated list of individual elements inside:

array = [element0, element1, element2, element3]

Nested arrays (arrays within arrays) are also possible:

nested = [elementB, elementC]
outer = [elementA, nested, element4]

The length of the outer array is 3 elements-long because the inner nested array counts as an individual element. By itself, the nested array is 2 elements-long.

Index Number

Array elements are usually referenced by an index number, which represents their position in the sequence.

The indices of most arrays start with 0:

groceryList = ["milk", "cookies", "berries", "carrots"]
// index:        0         1          2          3
  • The first element "milk" is at index 0.
  • The second element "cookies" is at index 1.
  • …and so on.

Arrays in Different Languages

Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn More on Codecademy