.. _more-lists: More on Lists ############# .. wordcount:: We learned a bit about lists in our last lecture. This time, we will see more on how useful these list things can be! *************** Creating a list *************** Let's start off by generating a list of numbers. The ``range`` function should be able to do that: .. code-block:: text >>> l = range(20) >>> print(l) range(0,20) Hmmm, that did not work like I expected it to! In fact ``range`` is a special function Python calls an ``iterable``. We will not study this kind of function in this class, but to generate the list, All I need to do is alter the way I try to build it: .. code-block:: text >>> l = list(range(10)) >>> print(l) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Now we are getting somewhere! Here is an interesting way to build up a list where all the elements are the same: >>> z = [0]*10 >>> print(z) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] I wonder what happens if I add two lists: .. code-block:: text >>> s = l + z >>> print(s) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] As we saw with strings, a list added to a second list gives a new list with the elements of both in the order specified. Another way to do the same thing is this: .. code-block:: text s = l s.extend(z) >>> print(s) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] This approach does not work (I am still scratching my head over this): .. code-block:: text >>> s = l.extend(z) >>> print(s) None I guess since the `extend` function does an in-place change, it returns no value. List slices *********** Suppose you wanted the sequence from 5 to 15. We can slice off that segment of this list by using the Python ``slice`` notation: .. code-block:: text >>> l = list(range(20) >>> s = l[5:16] >>> print(s) [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] There is that funny problem again. The slice range notation stops one short of the last value! We can do other kinds of slices: .. code-block:: text >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> l[:16] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] >>> l[5:] [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] If you leave off the first number in the slice rance, the initial number is zero. If you leave off the last number, the end point is the last item in the list. What do you think you get if you leave off both numbers? (Trick question!) .. code-block:: text l[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] Makes sense - right? **************************************** Lists can hold different kinds of things **************************************** Suppose we had a group of people and we wanted to store information about each one. We might do this: .. literalinclude:: code/MultiList.py :lines: 1-3 What would happen if we constructed a list of people in this group? .. literalinclude:: code/MultiList.py :lines: 5 Here is some code to print out data from this super list: .. literalinclude:: code/MultiList.py :lines: 12-18 Here is what we get when running this code: .. code-block:: python python3 MultiList.py [['Sarah', 'Fenway', 'f123456', 99], ['Richard', 'Penn', 'p234567', 89], ['Scott', 'Lauder', 'l54321l', 87]] ['Sarah', 'Fenway', 'f123456', 99] ['Richard', 'Penn', 'p234567', 89] ['Scott', 'Lauder', 'l54321l', 87] Richard Penn p234567 89 Scott That is not so bad, once you get used to remembering what each element in your list actually is. If it is another list, just tack on another set of square brackets to step inside that list! Graphics using Lists ******************** Let's look at a program that plots the sine of X where X ranges over values from 0 to 360 degrees. Here is the code that does that: .. literalinclude:: code/SinePlot.py :linenos: Here is what I got when running this: .. image:: images/sine-plot.png :align: center