CHAPTER 2 ■ NATIVE DATA TYPES
26
Removing Items from a List
Lists can expand and contract automatically. You’ve seen the expansion part, but there are also several
different ways to remove items from a list, as shown in Listing 2-15.
Listing 2-15. Removing Items from a List
>>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new']
>>> a_list[1]
'b'
>>> del a_list[1] (1)
>>> a_list
['a', 'new', 'mpilgrim', 'new']
>>> a_list[1] (2)
'new'
1. You can use the del statement to delete a specific item from a list.
2. Accessing index 1 after deleting index 1 does not result in an error. All items
after the deleted item shift their positional index to “fill the gap” created by
deleting the item.
What if you don’t know the positional index? That’s not a ...