Lists
Cypher® includes comprehensive support for lists.
For information about the list predicate operator IN , which checks for list membership, see Expressions → Predicates → List operators.
For information list concatenation (+ and || ), list element access and slicing ([] ), as well as list and pattern comprehensions, see List expressions.
|
Lists in general
A literal list is created by using brackets and separating the elements in the list with commas.
RETURN [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] AS list
list |
---|
|
Rows: 1 |
A list can consist of different value types.
RETURN [0, "hello", 3.14, null] AS list
list |
---|
|
Rows: 1 |
Lists are indexed by 0 in Cypher. To access individual elements in a list, use square brackets. This extracts from the start index and up to, but not including, the end index.
For example:
WITH [5,1,7] AS list
RETURN list[2]
list[2] |
---|
|
Rows: 1 |
List range and size
The below examples use the range
function to create lists.
This function returns a list containing all numbers between given start and end numbers.
The range is inclusive in both ends.
RETURN range(0, 10)[3] AS element
element |
---|
|
Rows: 1 |
It is also possible to use negative numbers, to start from the end of the list instead.
RETURN range(0, 10)[-3] AS element
element |
---|
|
Rows: 1 |
Finally, it is possible to use ranges inside the brackets to return ranges of the list.
The list range operator ([]
) is inclusive of the first value, but exclusive of the last value.
RETURN range(0, 10)[0..3] AS list
list |
---|
|
Rows: 1 |
RETURN range(0, 10)[0..-5] AS list
list |
---|
|
Rows: 1 |
RETURN range(0, 10)[-5..] AS list
list |
---|
|
Rows: 1 |
RETURN range(0, 10)[..4] AS list
list |
---|
|
Rows: 1 |
Out-of-bound slices are simply truncated, but out-of-bound single elements return null
.
RETURN range(0, 10)[15] AS list
list |
---|
|
Rows: 1 |
RETURN range(0, 10)[5..15] AS list
list |
---|
|
Rows: 1 |
The size
of a list can be obtained as follows:
RETURN size(range(0, 10)[0..3]) AS list
list |
---|
|
Rows: 1 |
Storing lists as properties
It is possible to store homogenous lists of simple values as properties.
CREATE (n:Label)
SET n.listProperty = [1, 2, 3]
RETURN n.listProperty AS homogenousListProperty
homogenousListProperty |
---|
|
Rows: 1 |
It is not, however, possible to store heterogeneous lists as properties.
CREATE (n:Label)
SET n.listProperty = [1, "hello", .45, date()]
RETURN n.listProperty AS heterogenousListProperty
Neo4j only supports a subset of Cypher types for storage as singleton or array properties. Please refer to section cypher/syntax/values of the manual for more details.