Lists

List is another data type, just like a number, string, or boolean value.

A list is an ordered and linear sequence of information. We need this order, so that the whole list makes sense and works as intended.

Each item in a list has its unique position. This unique position of each item is known as its index. Like a home address, this index is what’s used to describe the location of an item on the list.

When a list has more than one index, the list has indices (where indices = more than one index). List indices are denoted with numbers.

Generally, we start the numbering of a list’s indices at 0 and then add one for each successive value. That mean, the first index on a list has position 0, the second index has position 1, and so on.

Here’s an example of a list:

States = ['Georgia', 'New York', 'California'];

‘Georgia’ is at the first position, so it’s INDEX0, ‘New York’ is INDEX1, and on and on.

It’s important that we know the index of an item in a list. This would help us access any item that we wish to work on.

That way, we can:

  • select that item from the list, and do something with it (for example, save the item to a variable)
  • modify the whole list (for example, by adding to the list or removing from it)

How to Create a List… In JavaScript

How to create a list will vary depending on the programming language you’re using.

For example, in Javascript,

  • A list typically starts with the name of the list, for example, States
  • We then set the list equal to a set of square brackets []. If the square brackets have nothing in them, the list is empty.

Each item on the list is a string, for example ‘Georgia’.

Other than a string, we can also put other data types (like numbers, boolean values) into the lists. We can even put other lists in a bigger list!

We can single out or select an item from a list; by using the syntax:

States[0]

In the list States, the first item (the item with the index 0) is Georgia. So, the code will return the string ‘Georgia’.

Another example:

cities = ['boston', 'los angeles', 'austin', 'denver']

To choose the third item in the list, we would write:

cities[2]

… which will return ‘austin’.

We can then assign this selected item to a variable (that way, we can use it later in our code).

growingCity = cities[2]

Here, we’ve assigned the string ‘austin’ to the variable ‘growingCity’.

In JavaScript, we can edit lists with commands like .append, .splice, or .pop.

To recap

… how do I build a list, select an item from the list, and save it to a variable, all in JavaScript?

Here’s how:

  1. Build the list; for example, myLetters
  2. Note the position (or index) and select the item from the list; for example, position 1, which is the string ‘B’
  3. Save the item to a defined variable, for example, goodLetter

As shown below:

myLetters = ['A', 'B', 'C', 'D']

myLetters[1]

goodLetter = myLetters[1]

List Operations

Here are some operations we can do with a list:

  1. add items to the list, or
  2. remove items from the list

1. Adding Items to a List

We can add items at the end of a list, or insert the items within the list.

To add items to a list, we say that we’re appending the items at the end of that list.

For example, to add the item ‘E’ to my list of myLetters, I’d need to append the string ‘E’:

myLetters = ['A', 'B', 'C', 'D']

myLetters.append('E')

That way, the code will return:

myLetters == ['A', 'B', 'C', 'D', 'E']

I can also insert items within the list, by using the index number for where I want to place the new item(s).

For example, if I’d like to insert the item ‘X’ as the third item in the list myLetters, that means I’d have to place my ‘X’ after ‘B’ (INDEX1) but before ‘C’ (INDEX2).

So, I’ll have:

myLetters = ['A', 'B', 'C', 'D']

myLetters.splice (2, 1, 'X')

That is: place ‘X’ between 1 and 2.

That way, the code will return:

myLetters == ['A', 'B', 'X', 'C', 'D']

NOTE:

Inserting an item within the list will ‘mess up’ the initial order, and so we’d end up with new positions (or indices) for some of the items.

For example, in the myLetters code shown above,

‘A’ and ‘B’ retained their indices. ‘A’ – INDEX0 and ‘B’ – INDEX1.

However, ‘X’ is now INDEX2…

‘C’ is INDEX3 (formerly INDEX2)… and

‘D’ is now INDEX4 (formerly INDEX3).

2. Removing Items from a List

We can take the last item off a list, or use indexing to pin-point a specific item to remove from the list.

To remove the last item:

Say we have the list:

myLetters = ['A', 'B', 'C', 'D']

We can remove the last item ‘D’:

myLetters.pop()

So that the code returns:

myLetters == ['A', 'B', 'C']

To remove specific items:

Say we have the list:

myLetters = ['A', 'B', 'C', 'D']

We can remove the string ‘B’:

myLetters.splice(1,1)

So that the code returns:

myLetters = ['A', 'C', 'D']

Resources: 1

1 thought on “Lists”

Leave a Comment