1.5. Adding Elements To a List

Changing Elements in a List

The method for changing an element in a list is the same as the method for getting an element in a list. For example, we want to change the first element in languages to be German. We can simply use `=` and the desired index as the following example.

Adding Elements to a List

append()

The easiest way to add a new element is using the append() method. This will add a new element to the end of the list. We will try to add German and French to our original languages list.


insert()

This method is used if you want to add a new element to a particular position. We will try to add German as the first element and French as the third element (remember, we use index 0 and 2.)

del

This statement can be used if the position of the item you want to delete is known. For example, we want to delete the Indonesian from the languages list. This is the 3rd element, so we use index 2.

pop()

This statement can be used if the position of the item you want to delete is known. For example, we want to delete the Indonesian from the languages list. This is the 3rd element, so we use index 2.

remove()

Sometimes, we don't know the position of the element but we know the element name. In this case, we can use remove() method. Say we want to remove English from the language list.

len()

The length of a list can be obtained using len() function. Our languages list has six elements, so its length is 6.

> Unlike index, Python counts the items in a list starting with one. So, the last element of the list will have index len(list) - 1.

Exercise 1.5

  1. Guest List
    You are hosting a dinner and you are inviting Asta, Yuno, Noelle, and Mimosa. Make a list that include those names then use your list to print an invitation message to each person like this:

I'm inviting you, [friend's name], to come for dinner..

  1. Changing Guest List
    You just heard that Yuno can't make the dinner. Modify your list by replacing Yuno with Vanessa. Print a second set of invitation messages, one for each person who is still in your list.

  2. The Last Numbers
    You just found a bigger dinner table, so now more space is available.

  • Use insert() to add Charmy to the beginning of your list.

  • Use insert() to add Finral to the middle of your list.

  • Use append() to add Luck to the end of your list.

  • Print a new set of invitation messages, one for each person in your list.

  1. Shrinking Guest List
    You just found out that your new dinner table won't arrive in time for the dinner, and you have space for only two guests.

  • Use pop() to remove guests from your list one at a time until two names remain in your list. Each time you pop from your list, print a message to that person saying: "I'm sorry, [name], I can't invite you to dinner this time."

  • Print a message to each of the two people still on your list, saying, "[name], you are still invited."

  • Use del to remove the last two names from your list, so you have an empty list. Make sure print(guest_list) at the last line shows an empty list.