Making lists

Lots of values

Now that we know how to store values, what if we want to store multiple values, like a bunch of friends?

OK computer:
remember "Cece" as my_friend
remember "Jess" as my_friend
remember "Nick" as my_friend
display my_friend

//Output Below

Nick

Huh! It looks like my_friend is a variable and can only store a single value at a time.

Lots of variables

In that case, we'll create a variable for every single one of our friends!

Let's try and output a value from one here.

OK computer:
remember "Cece" as my_friend_1
remember "Jess" as my_friend_2
remember "Nick" as my_friend_3
display my_friend_3

//Output Below

Nick

So, if we have hundreds of friends, does that mean we have to create hundreds of variables?

Lists to the rescue

Instead of having three variables that store single names, we can make a list that holds all the names.

OK computer:
create a list of my_friends
add "Cece" to my_friends
add "Jess" to my_friends
add "Nick" to my_friends
display my_friends

//Output Below

Cece, Jess, Nick

Fantastic! This list stores "Cece", "Jess", and "Nick" under a single name.

Accessing values

Now that the values are on a list, how can we access them?

Let's try and display one of our friends.

OK computer:
create a list of my_friends
add "Cece" to my_friends
add "Jess" to my_friends
add "Nick" to my_friends
display the first item of my_friends

//Output Below

Cece

Just like that! We can point to the position of the value in the list.

Inserting values

Because we can insert values at specific positions, lists are quite flexible.

OK computer:
create a list of favorite_dishes
add "Curry" to favorite_dishes
add "Pizza" to favorite_dishes
add "Ramen" to favorite_dishes as the first item
display favorite_dishes

//Output Below

Ramen, Curry, Pizza

Nice! When we add or insert a value at a position, the other values simply move back.

Removing values

And, of course, we can remove values from lists as well.

Let's try and remove one item from our list here.

OK computer:
create a list of favorite_dishes
add "Curry" to favorite_dishes
add "Pizza" to favorite_dishes
add "Ramen" to favorite_dishes
remove the first item from the list
display favorite_dishes

//Output Below

Pizza, Ramen

Poof! We can either tell the computer to remove a specific value or an item at a specific position.

Going through lists

Alright. Now, let's take a look at why lists are so great, shall we?

OK computer:
repeat for every friend in my_friends:
 display "I like " and the friend

//Output Below

I like Cece
I like Jess
I like Nick

Sweet! We can create a loop to go through my_friends one-by-one.

Psst: again, we need to include a space between "I like" and the name.

Sorting things out

Another great thing about lists is that we can sort them.

OK computer:
sort my_friends in reverse alphabetical order
display my_friends

//Output Below

Nick, Jess, Cece

Sweet. That would've been a lot harder with single variables, eh?