Course Content
Python Data Structures
Python Data Structures
Deleting Elements
Imagine you have a to-do list:
tasks = ['email Tom', 'call Max', 'meet with sister'] print(tasks)
This list contains 3 items.
Let's say you've finished the first task on this list; meaning, you've already sent an email to Tom. To cross that task off the list, you'd write:
tasks = ['email Tom', 'call Max', 'meet with sister'] del tasks[0] print(tasks)
Now, the list is down to two tasks:
tasks[0]
reads'call Max'
;tasks[1]
reads'meet with sister'
.
You can remove any item from the list by using its index number.
Note
Curious about the syntax of
del
?
- The statement starts with the keyword
del
;- Next, you typically identify the list item with its index like
tasks[index]
.
Time to give it a shot!
Task
Here's your list:
groups = ['The Beatles', 'The Band', 'The Clash', 'The Dillards', 'The Mamas and the Papas', 'The Kingston Trio']
Your task is to remove the following bands from the list using positive indexing only: 'The Beatles'
and 'The Mamas and the Papas'
.
Stuck on which indexes to use for removal? Check out the hints.
Thanks for your feedback!
Deleting Elements
Imagine you have a to-do list:
tasks = ['email Tom', 'call Max', 'meet with sister'] print(tasks)
This list contains 3 items.
Let's say you've finished the first task on this list; meaning, you've already sent an email to Tom. To cross that task off the list, you'd write:
tasks = ['email Tom', 'call Max', 'meet with sister'] del tasks[0] print(tasks)
Now, the list is down to two tasks:
tasks[0]
reads'call Max'
;tasks[1]
reads'meet with sister'
.
You can remove any item from the list by using its index number.
Note
Curious about the syntax of
del
?
- The statement starts with the keyword
del
;- Next, you typically identify the list item with its index like
tasks[index]
.
Time to give it a shot!
Task
Here's your list:
groups = ['The Beatles', 'The Band', 'The Clash', 'The Dillards', 'The Mamas and the Papas', 'The Kingston Trio']
Your task is to remove the following bands from the list using positive indexing only: 'The Beatles'
and 'The Mamas and the Papas'
.
Stuck on which indexes to use for removal? Check out the hints.
Thanks for your feedback!
Deleting Elements
Imagine you have a to-do list:
tasks = ['email Tom', 'call Max', 'meet with sister'] print(tasks)
This list contains 3 items.
Let's say you've finished the first task on this list; meaning, you've already sent an email to Tom. To cross that task off the list, you'd write:
tasks = ['email Tom', 'call Max', 'meet with sister'] del tasks[0] print(tasks)
Now, the list is down to two tasks:
tasks[0]
reads'call Max'
;tasks[1]
reads'meet with sister'
.
You can remove any item from the list by using its index number.
Note
Curious about the syntax of
del
?
- The statement starts with the keyword
del
;- Next, you typically identify the list item with its index like
tasks[index]
.
Time to give it a shot!
Task
Here's your list:
groups = ['The Beatles', 'The Band', 'The Clash', 'The Dillards', 'The Mamas and the Papas', 'The Kingston Trio']
Your task is to remove the following bands from the list using positive indexing only: 'The Beatles'
and 'The Mamas and the Papas'
.
Stuck on which indexes to use for removal? Check out the hints.
Thanks for your feedback!
Imagine you have a to-do list:
tasks = ['email Tom', 'call Max', 'meet with sister'] print(tasks)
This list contains 3 items.
Let's say you've finished the first task on this list; meaning, you've already sent an email to Tom. To cross that task off the list, you'd write:
tasks = ['email Tom', 'call Max', 'meet with sister'] del tasks[0] print(tasks)
Now, the list is down to two tasks:
tasks[0]
reads'call Max'
;tasks[1]
reads'meet with sister'
.
You can remove any item from the list by using its index number.
Note
Curious about the syntax of
del
?
- The statement starts with the keyword
del
;- Next, you typically identify the list item with its index like
tasks[index]
.
Time to give it a shot!
Task
Here's your list:
groups = ['The Beatles', 'The Band', 'The Clash', 'The Dillards', 'The Mamas and the Papas', 'The Kingston Trio']
Your task is to remove the following bands from the list using positive indexing only: 'The Beatles'
and 'The Mamas and the Papas'
.
Stuck on which indexes to use for removal? Check out the hints.