.. note:: :class: sphx-glr-download-link-note Download `here `_ Data structures =============== *Any code you write in the workbook should be re-usable where possible, i.e. it should work if the values assigned to any variables are modified.* List indexing ------------- Remember that we can have: - any number of items in a list, separated by commas - different data types and structures within our list … and that the syntax for accessing elements is: - ``[position]`` - ``[start:stop]`` - ``[start:stop:step]`` .. code:: ipython3 presidents = ['Ronald Reagan', 'George H. W. Bush', 'William J. Clinton', \ 'George W. Bush', 'Barack Obama', 'Donald J. Trump'] #U.S. Presidents 1981-2020 **Assign to a variable called ``number_presidents`` the number of entries in (i.e. ``len``\ gth of) ``presidents``:** .. code:: ipython3 number_presidents = len(presidents) number_presidents .. parsed-literal:: 6 **Assign to a variable called ``last_president`` the final president in ``presidents``:** .. code:: ipython3 last_president = presidents[-1] last_president .. parsed-literal:: 'Donald J. Trump' .. code:: ipython3 n = 4 **Assign to a variable called ``nth_president``, the name of that president in the list:** - the first entry in ``presidents`` would be returned when ``n = 1`` (remember that Python is zero-indexed) - you can assume that ``n`` is always ``<= len(presidents)`` .. code:: ipython3 nth_president = presidents[n-1] nth_president .. parsed-literal:: 'George W. Bush' String indexing --------------- Remember that we can use indexing syntax in the same way on strings. **Assign to a variable called ``space_index``, the (zero-indexed) position of the first space character (``' '``) in ``nth_president``:** - use the ``.index()`` method - you can assume all entries contain a space .. code:: ipython3 space_index = nth_president.index(' ') space_index .. parsed-literal:: 6 **Using ``nth_president`` and ``space_index``, assign to a variable called ``name_nth_president`` a string of the characters in ``nth_president`` up to (but not including) the first space:** .. code:: ipython3 name_nth_president = nth_president[:space_index] name_nth_president .. parsed-literal:: 'George' List methods ------------ .. code:: ipython3 ratings = [7, 4, 8, 2, 9, 4, 3, 6, 8, 6, 7, 9, 2, 0, 3, 4, 1, 0, 2] today = [5, 8, 2] **Use ``.extend()`` to add the ``today`` list to ``ratings``:** .. code:: ipython3 ratings.extend(today) ratings .. parsed-literal:: [7, 4, 8, 2, 9, 4, 3, 6, 8, 6, 7, 9, 2, 0, 3, 4, 1, 0, 2, 5, 8, 2] **Use ``.sort()`` and then ``.reverse()`` to order the elements in ``ratings`` with the highest numbers coming first:** .. code:: ipython3 ratings.sort() ratings.reverse() .. code:: ipython3 ratings .. parsed-literal:: [9, 9, 8, 8, 8, 7, 7, 6, 6, 5, 4, 4, 4, 3, 3, 2, 2, 2, 2, 1, 0, 0] **Use ``.count()`` to assign the number of entries which equal ``0`` to ``zero_ratings``:** - Information on the ``.count()`` method and other list methods can be found in the `documentation `__ .. code:: ipython3 zero_ratings = ratings.count(0) zero_ratings .. parsed-literal:: 2 **Use ``.index()`` to assign to ``high_ratings`` how many ratings are ``> 7``:** - Your list must have been sorted correctly in the earlier question .. code:: ipython3 high_ratings = ratings.index(7) high_ratings .. parsed-literal:: 5 Dictionaries ------------ .. code:: ipython3 guest = {'name': 'Boris Johnson', 'country': 'United Kingdom', 'partner': {'name': 'Carrie Symonds', 'relationship': 'Engaged', }, 'age': 55, 'job': {'title': 'Prime Minister', 'years': 1, }, } **Assign to a variable called ``guest_country`` the value in the ``guest`` dictionary associated with the ``country`` key:** .. code:: ipython3 guest_country = guest['country'] guest_country .. parsed-literal:: 'United Kingdom' **Assign to a variable called ``partner_name`` the value for ``name`` from the dictionary which is itself the entry for ``partner`` in the ``guest`` dictionary:** .. code:: ipython3 partner_name = guest['partner']['name'] partner_name .. parsed-literal:: 'Carrie Symonds' **Add a new key:value pair to ``guest``, with a key of ``'order'`` and a value of ``'Roast Beef'``:** .. code:: ipython3 guest['order'] = 'Roast Beef' guest['order'] .. parsed-literal:: 'Roast Beef' **Change the value for ``relationship`` in ``partner`` to ``'Married'``:** .. code:: ipython3 guest['partner']['relationship'] = 'Married' guest['partner'] .. parsed-literal:: {'name': 'Carrie Symonds', 'relationship': 'Married'} **Use the ``.get()`` method to assign to a variable called ``guest_notes`` the value for the key ``'notes'`` in ``guest``, using ``'None found'`` as a fallback value if the key isn’t found:** .. code:: ipython3 guest_notes = guest.get('notes', 'None found') guest_notes .. parsed-literal:: 'None found' Tuples ------ **Assign to ``guest_tag`` a tuple containing three elements in the following order, with values from ``guest``:** - ``name`` - ``title`` (from inside the nested ``job`` dictionary) - ``country`` .. code:: ipython3 guest_tag = (guest['name'], guest['job']['title'], guest['country']) print(guest_tag[0] + '\n' + guest_tag[1] + '\n' + guest_tag[2]) .. parsed-literal:: Boris Johnson Prime Minister United Kingdom Sets ---- .. code:: ipython3 hobbies = {'drumming', 'cycling', 'chess', 'travelling'} **Use the ``.discard()`` method to discard ``'travelling'`` from ``hobbies`` and then the ``.add()`` method to add ``'swimming'``:** .. code:: ipython3 hobbies.discard('travelling') hobbies.add('swimming') hobbies .. parsed-literal:: {'chess', 'cycling', 'drumming', 'swimming'} Built-in functions ------------------ **Use the built-in function ``list()`` to create a list from ``hobbies``, assigning it to ``hobbies_list``:** .. code:: ipython3 hobbies_list = list(hobbies) hobbies_list .. parsed-literal:: ['cycling', 'drumming', 'swimming', 'chess'] **Use the built-in function ``sorted()``, and list indexing, to assign to ``first_hobby`` the item from ``hobbies_list`` which is first alphabetically:** .. code:: ipython3 first_hobby = sorted(hobbies_list)[0] first_hobby .. parsed-literal:: 'chess'