Python Lists
We’ve learnt that a string is basically an array of characters in a single variable, that can be accessed individually, or in groups, by using an index. But it’s not just characters that can be accessed in an array. Whole strings, integers or floating point numbers can also be used in this way, by putting them in a list.
Here’s a fascinating example:
1 |
a_list = [ "nigel", 7, "top hat", 3.4, "4" ] |
A list is just a collection of values in square brackets, each value separated by a comma. We still use speech marks (or single quotes) to represent strings, so “4” in the list above is actually a string, not an integer.
We can access values in the list just like we did in an array in the previous section:
1 2 3 4 5 6 7 8 9 10 11 |
shopping_list = [ 'apples' , 'toilet roll' , 'doughnuts' , 'plunger' , 'soup' , 'air freshener' , 'corn flakes' , 'indigestion tablets' ] print('apples' in shopping_list) print(shopping_list[1]) print(shopping_list[2] + " are better than " + shopping_list[4]) print(shopping_list[2][0]) print(shopping_list[2][5:]) new_list = shopping_list[1::2] + ['pasta'] print(new_list) new_list.remove('pasta') print(new_list) |
Notice how in lines 6 and 7 we even used an array twice, Once to find shopping list item 3 (array position 2), and then again to find some characters within the list item found. This is a two-dimensional array. You can also add and remove from lists, as you can see in lines 8 and 10.
We can use variables to store array position numbers, which means we can do anything with the array position that we can do with any other variable. Here’s a program to generate poetry:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#---------------- #Poetry generator #---------------- #allows us to generate random numbers import random #the 1st part of the poem can be one of 4 words part1 = ['Tears','Fear','Happiness','Life'] #the 2nd part of the poem is chosen between these 4 parts part2 = ['like a','for the','near the','against a'] #the 3rd part of the poem can be one of 4 words part3 = ['mountain','shadow','heartache','briefcase'] #the 4th part of the poem is chosen between these 4 parts part4 = ['of a lifetime','of my heart','near my face','in a reflection'] #this randomly decides the 1st part of the poem... rand1 = random.randint(0,len(part1) - 1) #...and the 2nd part... rand2 = random.randint(0,len(part2) - 1) #...and the 3rd part... rand3 = random.randint(0,len(part3) - 1) #...and the 4th part rand4 = random.randint(0,len(part4) - 1) #add together the randomly selected array elements poem = part1[rand1] + ", " + part2[rand2] + " " + part3[rand3] + " " + part4[rand4] #print the poem print(poem) print("...You're welcome.") |
This program produces such moving poems as:
Fear, like a heartache near my face
...You're welcome.
So deep.
Even though each part of the poem has 4 possible choices, why did we write:
1 2 |
#this randomly decides the 1st part of the poem... rand1 = random.randint(0,len(part1) - 1) |
when we could have just written:
1 2 |
#this randomly decides the 1st part of the poem... rand1 = random.randint(0,3) |
At the moment, they both do the same thing. They both pick a random number between 0 and 3 (to choose one of the four possible parts). The first method does this by finding the length of the list (4) and then subtracting 1 (to get 3). Isn’t that just a long way of doing something simple? Well, yes it is. But what if we add more text options to each part of the poem, like this:
1 2 |
#the 1st part of the poem can be one of __NOW SIX__ words part1 = ['Tears','Fear','Happiness','Life','Cheese','Computing'] |
Now the easy way doesn’t work properly, unless we change the first random number generator to be extended to take the newly added words into account:
1 2 |
#this randomly decides the 1st part of the poem... rand1 = random.randint(0,5) |
…but the other way would still work:
1 2 |
#this randomly decides the 1st part of the poem... rand1 = random.randint(0,len(part1) - 1) |
It would take the length of the list (now 6) and subtract 1 (giving a random number between 0 and 5). This is called future-proofing, meaning that the code doesn’t need to be changed in the future if other parts of our program change. Programming like this will save you a lot of time, and stop your whole program from breaking because you’ve added something small.
Challenge
Write a program to store items in an inventory for an RPG computer game. Your character can add items (like swords, shields and various potions) to their inventory, as well as ‘use’ an item, that will remove it from the inventory list.
You could even have other variables like ‘health’ and ‘strength’, that are affected by ‘finding’ or ‘using’ items in the inventory.