1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Network Analysis in Python

Connected

Exercise

Time filter on edges

You're now going to practice filtering the graph using a conditional as applied to the edges. This will help you gain practice and become comfortable with list comprehensions that contain conditionals.

To help you with the exercises, remember that you can import datetime objects from the datetime module. On the graph, the metadata has a date key that is paired with a datetime object as a value.

Instructions

100 XP
  • Instantiate a new graph called G_sub.
  • Add nodes from the original graph (including the node metadata), using the .add_nodes_from() method.
  • Add edges using a list comprehension with one conditional on the edge dates, that the date of the edge is earlier than 2004-05-16. To do this:
    • Use the .add_edges_from() method with a list comprehension as the argument.
    • The output expression of the list comprehension is (u, v, d). Iterate over all the edges of G and check whether d['date'] is less than datetime(2004, 5, 16).