Page 43 - Programming the Raspberry Pi Getting Started with Python
P. 43
If you want to sort the list, you can do this:
To remove an item from a list, you use the command pop, as shown next. If you do not specify an
argument to pop, it will just remove the last element of the list and return it.
If you specify a number as the argument to pop, that is the position of the element to be removed.
Here’s an example:
As well as removing items from a list, you can also insert an item into the list at a particular
position. The function insert takes two arguments. The first is the position before which to insert,
and the second argument is the item to insert.
When you want to find out how long a list is, you use len(numbers), but when you want to sort the
list or “pop” an element off the list, you put a dot after the variable containing the list and then issue
the command, like this:
These two different styles are a result of something called object orientation, which we will discuss
in the next chapter.
Lists can be made into quite complex structures that contain other lists and a mixture of different
types—numbers, strings, and logical values. Figure 4-2 shows the list structure that results from the
following line of code: