Section 8: Lists Continued

Welcome to Section 8! In this section, we will cover the more advanced functions for lists. Let's get started!

8.1 Del and Remove

We will often want to delete something from a list based on a specific index. To do that, we can use the del function. Here is an example ...

numbers = ["s", "t", "i", "r", "i", "n", "g"]

del numbers[2]

print(numbers)

The code above will print numbers = ["s", "t", "r", "i", "n", "g"]. This is because the second line of the code deletes the item that is at index 2. The syntax for del is slightly different than other functions. You start by typing del, then, you type the list's name with a set of brackets after it, and finally, you type the index you want removed between the brackets.

However, sometimes, we want to remove an item even if we don't know its index. To do that, we can use the remove function. Here is an example ...

numbers = ["s", "t", "i", "r", "i", "n", "g"]

numbers.remove("i")

print(numbers)

The code above will print numbers = ["s", "t", "r", "i", "n", "g"]. This is because the second line of the code removes the first instance of "i" from the array. Since it only removes the first instance of "i", the "i" at the end of the list stays the same. The syntax of remove is quite simple. You first type the list's name, then, you type .remove(), and finally, you type the item you want removed.

Now that you have del and remove down, let's move on to the next part!

8.2 Append and Insert

We will often find ourselves in the situation that they need to add something to the end of a list. To do that, we can use the append function. Here is an example ...

numbers = [1, 2, 3]

numbers.append(4)

print(numbers)

The code above will print [1, 2, 3, 4]. This is because the second line of the code above will add 4 to the end of the list numbers. The syntax of the append function is the same as the syntax of the remove function. However, instead of deleting the input, it will add the input.

Even though append is very helpful, append can't add something to the middle of a list. To do this, we can use insert. Here is an example ...

numbers = [1, 2, 4]

numbers.insert(2, 3)

print(numbers)

The code above will print [1, 2, 3, 4]. This is because the second line of the code above will add 3 at index 2 and what was originally at index 2 will be pushed back one index. The syntax of this is the same as append, but instead of just one argument in the parentheses, there are two, the index and the item.

Now that you have append and insert down, let's move on to the next part!

8.3 Sort

Another thing that is extremely important to know how to do is to sort lists. To do that, we can use the sort function. Here are some example ...

numbers = [-4.6, 2.5, 3, -1]

numbers.sort()

print(numbers)

letters = ["a", "c", "b", "d"]

letters.sort()

print(letters)

The code above will first print [-4.6, -1, 2.5, 3]. This is because the second line of the code above will sort the floats and integers into increasing order. The code above will then print ["a", "b", "c", "d"]. This is because the fifth line of the code above will sort the letters into alphabetical order. This will also work with words, but remember, capital letters and words will always be first.

On the other hand, sometimes, we want to sort backwards. To do that we can change the settings on our sort function. Here are some examples ...

numbers = [-4.6, 2.5, 3, -1]

numbers.sort(reverse = True)

print(numbers)

letters = ["a", "c", "b", "d"]

letters.sort(reverse = True)

print(letters)

The code above will first print [3, 2.5, -1, -4.6]. This is because when you set reverse to True, it will go in decreasing order. The code above will then print ["d", "c", "b", "a"]. This is because it will go in reverse alphabetical order. Remember, capitals will now come at the end. All I had to do was type reverse = True between the two parentheses.

Now that you have sort down, let's move on to the next part!

8.4 Index

We often need the index of an item for various list operations. To get an item's index, we can use the index function. Here is an example ...

letters = ["a", "b", "c", "d"]

print(numbers.index("b"))

The code above will print 1. This is because "b" is at index 1 in the list (second position from the left). Remember, if there is more than one occurrence of the item, only the first occurrence's index will be outputted. The syntax for the index function is the same as the syntax for the remove function.

Now that you have index down, let's move on to the next section!

Sample Problems