Deleting ElementsDeleting Elements

Suppose we have a list of things to do:

This list has 3 elements.

Let's say that we completed the first thing from this list; that is, we have already written an email to Tom. To strike that element off the list, we write:

Now the list has two elements:

tasks[0] is 'call Max'

tasks[1] is 'meet with sister'

We can delete any list item by specifying its index number.

Note

What about the syntax of the del?

  • The statement begins with the keyword del.
  • Then we usually specify the list item using index tasks[index].

It's time to practice!

Task

You have the following list:

groups = ['The Beatles', 'The Band', 'The Clash', 'The Dillards', 'The Mamas and the Papas', 'The Kingston Trio']

You have to drop such elements from the list using positive indexing only: 'The Beatles', 'The Mamas and the Papas'.

If you're having trouble with which indexes to use to delete the elements, look at the hints.

Everything was clear?

Section 1. Chapter 10
toggle bottom row