Game Development Reference
In-Depth Information
'Alice said hello to Bob.' into the shell. This expression will evaluate to
True .
>>> 'hello' in 'Alice said hello to Bob.'
True
>>>
Removing Items from Lists with del Statements
You can remove items from a list with a del statement. ("del" is short for "delete.") Try
creating a list of numbers by typing: spam = [2, 4, 6, 8, 10] and then del
spam[1] . Type spam to view the list's contents:
>>> spam = [2, 4, 6, 8, 10]
>>> del spam[1]
>>> spam
[2, 6, 8, 10]
>>>
Notice that when you deleted the item at index 1, the item that used to be at index 2
became the new index 1. The item that used to be at index 3 moved to be the new index 2.
Everything above the item that we deleted moved down one index. We can type del
spam[1] again and again to keep deleting items from the list:
>>> spam = [2, 4, 6, 8, 10]
>>> del spam[1]
>>> spam
[2, 6, 8, 10]
>>> del spam[1]
>>> spam
[2, 8, 10]
>>> del spam[1]
>>> spam
[2, 10]
>>>
Just remember that del is a statement, not a function or an operator. It does not evaluate
to any return value.
 
Search WWH ::




Custom Search