Session Ready
Exercise

Creating namedtuples for storing data

Often times when working with data, you will use a dictionary just so you can use key names to make reading the code and accessing the data easier to understand. Python has another container called a namedtuple that is a tuple, but has names for each position of the tuple. You create one by passing a name for the tuple type and a list of field names.

For example, Cookie = namedtuple("Cookie", ['name', 'quantity']) will create a container, and you can create new ones of the type using Cookie('chocolate chip', 1) where you can access the name using the name attribute, and then get the quantity using the quantity attribute.

In this exercise, you're going to restructure the transit data you've been working with into namedtuples for more descriptive code.

Instructions
100 XP
  • Import namedtuple from collections.
  • Create a namedtuple called DateDetails with a type name of DateDetails and fields of 'date', 'stop', and 'riders'.
  • Create a list called labeled_entries.
  • Iterate over the entries list, unpacking it into date, stop, and riders.
  • Create a new DateDetails namedtuple instance for each entry and append it to labeled_entries.
  • Print the first 5 items in labeled_entries. This has been done for you, so hit 'Submit Answer' to see the result!