Lists are a very powerful idea in many programming languages. In Python, Lists are one of the most important built-in tools of the language. You need to master lists. Let’s start with why we need them.
Imagine you have a small number of things you want to track. Let’s use our vague computer game we’ve been using for an example. The game has 5 players, friends that get together over the internet to play a dungeon game.
Now, if you’re the coder of this game you could keep track of each player’s healthScore by have 5 different variables. (for players Zero to Four)
playerZeroHealthScore = 100
playerOneHealthScore = 100
playerTwoHealthScore = 100
playerThreeHealthScore = 100
playerFourHealthScore = 100If we setup these 5 variables, our game can track 5 players! But we’d have to change the game’s code to track SIX players. Well, that not good. Kind of silly actually.
To get around this kind of problem we use a list. We could ask, "how many players are playing?", and then make the list that size. We know we need to track each player’s healthScore, so we create an list:
playerHealthScores = [100, 100, 100, 100, 100]Now, like a string, list indexes start at zero.
# 0 1 2 3 4
playerHealthScores = [100, 100, 100, 100, 100];This list is a data structure - a way for us to keep track of lots of data in a controlled fashion. (We can make lists any size, a list with a million things in it is not unreasonable.) If we need to deduct health points from one of the players, we can do something like this:
majorHit = 50
playerHealthScores[2] = 67 # player 2 just took a hit!
playerHealthScores[1] = 105 # player one is getting stronger.
playerHealthScores[3] = playerHealthScores[3] - majorHit
# the list now is [100, 105, 67, 0, 100]The best way to think about a list is like all those postal boxes at the post office. Each box has a number on it, and things get put in the box depending on the box number.
Lists are indexed like that. Each list spot has an index number, starting at zero. See how we use the number 2 to index into the playerHealthScore list?
Lists:
-
Can store multiple values in a single variable
-
Start counting from index position zero
-
Elements can be primitive data types or/and Objects
So let’s think about an list of donuts for the following examples.
Declaring and initializing some lists in Python:
donuts = ["chocolate", "glazed", "jelly"
listofLetters = ['c','h','r','i','s']
mixedData = ['one', 2, True] # a string, a number and a boolean!We use square brackets to get elements by their index. We’ll use an list of strings to identify our donuts. Sometimes, we say something like "donuts sub 2" to mean donuts[2].
donuts = ["chocolate", "glazed", "jelly"]
print(donuts[0]) # "chocolate" (we could say "donuts sub zero")
print(donuts[2]) # "jelly"We can also add things to the end of the list.
donuts = ["chocolate", "glazed", "jelly"]
donuts.append("strawberry") # notice there is no element 3 before this,
print(donuts) # but after, there are now 4 things in the list.We can use the length property to find the size of an array.
donuts = ["chocolate", "glazed", "jelly"]
print(len(donuts)) # it'll print 3Note: A string is an LIST of single characters
If we use the len() function carefully, we can always get the last element in a list. The index of the last element in the list is 'length minus 1'.
donuts = ["chocolate", "glazed", "jelly"]
donuts("strawberry") # -> ["chocolate", "glazed", "jelly", "strawberry"]
print(donuts[len(donuts) - 1]) # strawberry
donuts.append("powdered") # -> ["chocolate", "glazed", "jelly", "strawberry", "powdered"]
print(donuts[len(donuts) - 1]) # powderedSome computer languages have arrays. Python’s Lists are like arrays and even more powerful.