Using list comprehension to aggregate data
List comprehension enables you to extract values from an existing list. You can create a list by evaluating an expression that tests for list inclusion. In addition, you can modify the values returned in the list.
Here is an example where we simply return a subset of the list where the values in the countries list property are either USA or Germany or both:
MATCH (m:Movie)
RETURN m.title AS MovieTitle,
[x IN m.countries WHERE x CONTAINS 'USA' OR x CONTAINS 'Germany' | x]
AS Country LIMIT 500
Notice that for this evaluation, we use CONTAINS. This is because some of the data in the lists have leading blanks and we want to make sure we account for all values in the list.
Here is another example where we modify the values that are returned in the list:
MATCH (m:Movie)-[:ACTED_IN]-(a:Actor)
WHERE a.name = 'Tom Hanks'
WITH m ORDER by m.released DESC
WITH collect(m) AS Movies
WITH [x IN Movies | x.title + ": " + toString(date().year - date(x.released).year + 1)] AS Summary
RETURN Summary
In this code we are transforming the list to contain the title of the movie and the number of years since it was released in descending order.
Check your understanding
1. Using list comprehension
What are some reasons why you would use list comprehension in your queries?
-
❏ To count the number of elements in the list.
-
✓ To select a subset of the list based upon some criteria.
-
❏ To turn the elements of a list into a single string.
-
✓ To transform some or all elements in a list to new values.
Hint
There are two good use cases for using list comprehension in Cypher.
Solution
You use list comprehension to select a subset of a list based upon some criteria or to transform some or all elements of a list into new values.
2. Titles and Release Dates for Actors
We want to return the list of titles and release dates for the movies of an actor. Each row returned will contain the name of the actor and a list containing the title and release date for a movie. The title and release date element will be of the format "title: release date". Use the dropdown below to select the correct code to complete this query.
Once you have selected your option, click the Check Results query button to continue.
MATCH (a:Actor)--(m:Movie)
WITH a, collect (m) AS Movies
RETURN a.name AS Actor, size(Movies) as NumMovies,
/*select:[x IN Movies | x.title + ": " + toString(x.released)] AS Summary*/
LIMIT 100
-
❏
[x IN Movies | x.title + x.released] AS Summary
-
❏
[x IN Movies | x.title , x.released] AS Summary
-
✓
[x IN Movies | x.title + ": " + toString(x.released)] AS Summary
-
❏
[x IN Movies | x.title , toString(x.released)] AS Summary
Hint
Each element of the list returned for an actor will be a string.
Solution
The correct answer is [x IN Movies | x.title + ": " + toString(x.released)] AS Summary
Summary
In this lesson, you reviewed and learned more about list comprehension to aggregate data in your Cypher queries. In the next Challenge, you will create a query using list comprehension.