Python Lists

Other programming languages have something called an array. This is a data structure that can hold more than one value. Python also has this type of data structure. In Python, they are called lists rather than arrays. Let's see how they work.

Start a new project for this and add a Python file to it. (You should know how to do this by now.)

To create a list, you set up a variable in the normal way. After an equals sign, type a list of values between square brackets. Each item in your list is separated by a comma. Add this to your coding window:

superheroes = ["Jessica Jones", "Kamala Khan", "Luke Cage", "Matt Murdock"]

Just like strings, lists can be accessed via an index number. In the list above, superheroes[0] refers to Jessica Jones while superheroes[3] refers to Matt Murdock.
What we'll do now is to ask a user to choose a superhero. A user can type 1, 2, 3 or 4. The output window will then display the user's choice. We'd like to have this in the output window:

A list of items in the Python output window

The user can then type 1, 2, 3 or 4 to select an item on the list

To get an output window like the one above, you can use the newline characters. In Python, these are \n. You type them where you want your newline to appear. This code, for example:

hero = input("Choose a Hero \n 1) Jessica Jones")

would get you two lines:

Choose a Hero
1) Jessica Jones

Add another option by typing a new one on the end:

hero = input("Choose a Hero \n 1) Jessica Jones \n 2) Kamala Khan")

Wherever the \n characters are, you get a line break. Add two more:

hero = input("Choose a Hero \n 1) Jessica Jones \n 2) Kamala Khan \n 3) Luke Cage \n 4) Matt Murdock \n")

Your coding window should look like this:

Python list in an output window

If a user types 1 in the output window, we want the first item from our list. However, the first item in a Python list is zero, the next item 1, the next 2, and so on. The first item in the list would be:

superheroes[0]

But we haven't got a 0 in our output window for users to choose. The first number is 1, Jessica Jones.

The trick here is to deduct 1 from whatever the user types. Add this line to your code, then:

indexNumber = int(hero) - 1

This sets up a new variable called indexNumber. On the right side of the equals sign, we convert the hero variable to an integer, just like we did in a previous section. We then deduct 1 from this value.

Finally, we can print out the results:

print("You selected:", superheroes[indexNumber])

Our list is called superheroes. We're using an index number between square brackets to pull an item from the list. The index number comes from a user typing in to a console.

Run your code and try it out. You should see this, first:

A Python list of items to select

Click inside your output window, just after 4) Matt Murdock. Enter a number from 1 to 4:

An item selected from a list

Now press the enter key on your keyboard. You should see this:

List selection in Python

 

Python Lists - Concatenation

You can join one list to another list. Suppose we had a list of even numbers:

evenNumbers = [2, 4, 6, 8, 10]

And we had another list:

evenMoreNumbers = [12, 14, 16]

You can join the two together with the concatenation symbol ( + ):

print(evenNumbers + evenMoreNumbers)

If you run the above code, you should see the even numbers from 2 to 16 appear in the output window.

 

Python Lists are mutable

Unlike strings, lists in Python are mutable. This means that you can change the value of an item in your list. Suppose, for example, you wanted to change Matt Murdock in your list of superheroes to Frank Castle. Murdock is fourth on the list which, because index numbers start counting at zero, is superheros[3]. To change the value inside of superheros[3], type it on the left of an equal sign. On the right-hand side of the equals sign, type your new value:

superheros[3] = "Frank Caste"
print(superheros)

Notice that when you're printing out a list, you don't need the square brackets, just the name of your list.

 

Adding items to your Python Lists

You can add items to your lists. To do this, you need append.

superheroes.append("Matt Murdock")

Notice that you don't need an equals sign, because you're not assigning a value to a variable, here. All you need, after typing a dot after your list, is the word append. In between round brackets, you put what it is you want to add to your list. In the code above, we're adding Matt Murdock back in. Try the following code and see what prints out, and when:

superheroes = ["Jessica Jones", "Kamala Khan", "Luke Cage", "Matt Murdock"]
superheroes[3] = "Frank Castle"
print(superheroes)
superheroes.append("Matt Murdock")
print(superheroes)

 

List Slicing

You can use the techniques you learned in the string section with lists. In the code below, we're creating a new list from the superheroes one:

newList = superheroes[2:]
print(newList)

The code takes items from the superheroes list starting at position 2, which is actually the third item in the list (lists start counting at zero, remember). We didn't put anything after the colon because we wanted to grab the items to the end of the superheroes list.

If you want to clear a list completely, you can use the colon between a pair of square brackets. To the right of an equals sign, type a pair of empty square brackets:

superheroes[:] = [ ]

 

We'll leave lists there for the moment. But they are very useful, and we may need them again later. In the next section, you'll learn about Python functions.

Python Functions >