Arrays
Note: This lesson covers JavaScript and Python.
Definition
The basic structure to store several values in a sequence is called an array
in JavaScript and a list
in Python. Both are used to hold multiple
elements under a single name.
Syntax
arrays/list
There are various ways of creating an array/list.
- Literal - syntax takes a list of elements separated by a comma and enclosed in square brackets.
- General - you can create an empty array/list and add the elements individually.
- Literal
var array_name = [element0, element1, element2,... elementN];
- General
var array_name = []; array_name[0] = element0; array_name[1] = element1; array_name[2] = element2; ... array_name[N] = elementN;
- Literal
list_name = [element0, element1, element2,... elementN]
- General
list_name = [] list_name[0] = element0 list_name[1] = element1 list_name[2] = element2 ... list_name[N] = elementN
Note: Reminder that this lesson covers similar structures - an
array
in JavaScript and a list
in Python.
Access / Change value by index
marks | Value | 76 | 34 | 65 | 25 | 49 | 57 |
---|---|---|---|---|---|---|---|
Index | 0 | 1 | 2 | 3 | 4 | 5 |
- Create array/list - In our example below, we create an array/list named
marks
which has six elements. - Access value by index - We can access a value by referring to its index number. Indexes start at zero:
-
marks[0]
is the first value - 76. -
marks[1]
is the second value - 34. -
marks[5]
is the last value - 57.
-
- Change value by index - We can change a value by specifying the index number and the new value.
Our example selects index zero,
marks[0]
, to modify the first value in the list.
- Create Array
var marks = [76, 34, 65, 25, 49, 57];
- Access value by index
alert(marks[0]); // 76 alert(marks[1]); // 34
- Change value by index
marks[0] = 80; // [80, 34, 65, 25, 49, 57]
- Create List
marks = [76, 34, 65, 25, 49, 57]
- Access value by index
print(marks[0]) # 76 print(marks[1]) # 34
- Change value by index
marks[0] = 80 # [80, 34, 65, 25, 49, 57]
Adding end element
Each language has a built in method to add an element to the end of the array/list.
JavaScript uses push()
and Python uses append()
.
var marks = [76, 34, 65, 25, 49, 57];
marks.push(18); // [76, 34, 65, 25, 49, 57, 18];
marks.push(79, 31); // [76, 34, 65, 25, 49, 57, 18, 79, 31];
JavaScript Note:
- The JavaScript
push()
method adds new items to the end of an array and returns the new length. - The
push()
function can be used to append more than one value to an array in a single call:marks.push(79, 31);
marks = [76, 34, 65, 25, 49, 57]
marks.append(18)
# [76, 34, 65, 25, 49, 57, 18]
Python Note:
- The Python
append()
method adds an item to the end of a list. The method takes a single argument. - It differs from the JavaScript push() method as it does not return the new length and does not allow for adding multiple elements.
Removing end element
Each language has a built in method to remove an element from the end of the array/list.
var marks = [76, 34, 65, 25, 49, 57]
// remove last value
marks.pop(); // [76, 34, 65, 25, 49]
JavaScript Note:
- The
pop()
method removes the last value of an array and returns that value. - The JavaScript
pop()
method does not have any parameters and you cannot pass it an index (unlike Python pop()).
marks = [76, 34, 65, 25, 49, 57]
# remove last value
marks.pop() # [76, 34, 65, 25, 49]
#remove value at index 1
marks.pop(1) # [76, 65, 25, 49]
Python Note:
-
The Python
pop()
with an empty parameter will remove the last value. - Returns the element removed.
- The Python
pop()
method can take an optional single argument to specify the index of the value to remove.
Length of array/list
The example code below shows how the number of elements in the array/list can be determined.
This example also shows that a JavaScript array
and Python list
can contain elements of different types.
var results = ["Exam", 76, 34, 65, 25, 49, 57];
alert(results.length); // 7
results = ["Exam", 76, 34, 65, 25, 49, 57]
print(len(results)) # 7
Python Note: The Python
len()
function can be used to determine the number of items found
in the list. The list is passed as the argument.