Lesson 8: Lists
Creating a list
To initialise a list, place the elements, separated with commas, between square brackets. Here is an example of a list that represents a shopping basket and the elements that are contained in it:
shopping_basket = ["bread", "milk", "eggs", "tea", "bread", "apples", "carrots"]
The elements in this list are strings, so each one is contained within quotes, but it is possible to store different data types within the one list. In the following example, the list contains strings, an integer, a Boolean, and a floating point decimal:
random_list = [72, "elephant", 99.99, False, "27"]
It is also possible to initalise an empty list.
another_list = []
You can populate and change this list further along in your code.
Querying the contents of a list
To output the contents of a list to the screen, you can use the print command.
print(shopping_basket)
or
print("The contents of the shopping basket are", shopping_basket)
print("The number of occurrences of \"bread\" in the shopping basket is ", shopping_basket.count("bread"))
Note how we use the backslash character to avoid the quotation mark before the word "bread" being interpreted as the closing quote of the text being printed.
If there are no instances of the element in the list, it returns zero.
print("The number of occurrences of \"coffee\" in the shopping basket is ", shopping_basket.count("coffee"))
Each position in a list can be identified and referenced by its index. The first position has index 0, the second position has index 1, the third position has index 2, and so on.
print("The index of \"eggs\" in the shopping basket is", shopping_basket.index("eggs"))
Test yourself! How could we tweak the code of this example so that it returns the position of eggs in the list rather than its index?
You can return the element at a specific index in a list.
print("The element in index 4 of the shopping basket is", shopping_basket[4])
Index -1 always refers to the last element in a list.
print("The last element in the shopping basket is", shopping_basket[-1])
Just like when using a string, you can get the length of a list by using the len()
function.
print("The length of the list is", len(shopping_basket))
You can use an index range to get the list of elements in an index range.
print("The elements in index range 3 to 5 of the shopping basket are ", shopping_basket[3:5])
Making changes to a list
Using the integrated functions and methods, you can change the contents of a list.
Adding elements to a list
The append() method appends an element to the end of a list.
shopping_basket.append("sugar")
print("After adding \"sugar\" to the list, the contents of the shopping basket are ", shopping_basket)
To use the insert() method, you need to give it the index into which you want to put the element, as well as the element itself.
The existing element at that index, and any other elements in the list are moved along one place in the list to accommodate the new element.
shopping_basket.insert(2, "spinach")
print("After adding \"spinach\" to index 2 of the list, the contents of the shopping basket are ", shopping_basket)
After "spinach" is placed into index 2 of the list, "eggs" moves to index 3, "tea" moves to index 4, and so on. No information is lost.
There is an alternative way of placing an element in a particular index, This method is different from the previous method because it overwrites whatever is in that index. In the following example, "pineapple" replaces "carrots":
shopping_basket[7] = "pineapple"
print("After adding \"pineapple\" to index 7 of the list, the contents of the shopping basket are ", shopping_basket)
After "pineapple" is added to index 7, "carrots" is overwritten and "sugar" remains at index 8.
Removing elements from a list
You can remove an element from a list by using just the name of the element you want to remove. If there is more than one instance of this element, then only the first one is removed.
shopping_basket.remove("bread")
print("After removing the first instance of \"bread\", the contents of the shopping basket are ", shopping_basket)
An alternative way of removing an element from a list is by deleting the element at a specific index.
del shopping_basket[4]
print("After removing the other instance of \"bread\", the contents of the shopping basket are ", shopping_basket)
The clear()
method deletes all of the elements from the list.
shopping_basket.clear()
print ("The contents of the shopping basket after it has been cleared is ", shopping_basket)
Making further changes to a list
To reverse the order of the list, you can use the reverse()
method, but be aware that doing so permanently changes the list.
shopping_basket.reverse()
print("The contents of the shopping basket have been reversed: ", shopping_basket)
You can put the elements in the list into alphabetical order by using the sorted()
function. In the following example, the contents of the list are printed out in alphabetical order, but the list itself is not permanently changed:
print("The contents of the shopping basket in alphabetical order are ", sorted(shopping_basket))
To permanently sort the list elements into alphabetical order, use the sort()
method. This permanently changes the order of the list.
shopping_basket.sort()
print("The contents of the shopping basket have been sorted into alphabetical order: ", shopping_basket)
You can make a copy of a list. In this example, we want to save the current contents of the shopping basket and use it as a shopping list for the next time we need to do shopping.
shopping_list_next_week = shopping_basket.copy()
print ("The contents of next week's shopping list are", shopping_list_next_week)
Resources