You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For removing `NULL` values when concatenating `LIST` values, see xref:expressions/list-expressions.adoc#null-list-concatenation-list-comprehension[`NULL`, list concatenation, and list comprehension].
134
35
135
36
[[list-element-access]]
136
37
== List element access
@@ -202,6 +103,44 @@ RETURN nestedList[1] AS secondList
The xref:expressions/predicates/list-operators.adoc[`IN`] operator, which checks for `LIST` membership, can be used together with `[]` to test whether an element exists in a nested `LIST`.
206
145
207
146
.Check for membership in a nested `LIST`
@@ -221,6 +160,7 @@ RETURN 3 IN nestedList[0] AS elementPresent
221
160
1+d|Rows: 1
222
161
|===
223
162
163
+
224
164
Attempting to reference an element outside the bounds of the `LIST` will return `NULL`, as will attempting to access elements from an empty `LIST`.
For removing `NULL` values when concatenating `LIST` values, see xref:expressions/list-expressions.adoc#null-list-concatenation-list-comprehension[`NULL`, list concatenation, and list comprehension].
327
+
328
+
[[add-elements]]
329
+
=== Add elements to a list
330
+
331
+
The `+` operator can add elements to the beginning or end of a `LIST` value.
332
+
This is not possible using the `||` operator.
333
+
334
+
.Add elements to the beginning and end of a `LIST`
To insert a `LIST` value into a nested `LIST`, the added `LIST` must itself be nested.
353
+
If the added `LIST` is not nested, its elements are treated as individual elements, whereas if it is nested, it maintains the `LIST` structure within the nested `LIST`.
List comprehension is used to create new `LIST` values by iterating over existing `LIST` values and transforming the elements based on certain conditions or operations.
398
+
This process effectively maps each element in the original `LIST` to a new value.
399
+
The result is a new `LIST` that consists of the transformed elements.
332
400
333
401
.Syntax
334
402
[source, syntax]
@@ -388,7 +456,28 @@ RETURN [n IN list WHERE n > 2 | n] AS filteredList
388
456
1+d|Rows: 1
389
457
|===
390
458
391
-
This below query iterates over the `skills` property of each Person node and creates a new `LIST` by xref:expressions/string-operators.adoc[concatenating the `STRING`] `" expert"` to each element in `skills`.
459
+
The next example shows how to map a `LIST` using its indexes with a list comprehension.
460
+
The xref:functions/list.adoc#functions-range[`range()`] function is used to generate the indexes from `0` to the last valid index of the `LIST`, and then each index is combined with its corresponding `LIST` value into a `STRING` value.
461
+
The result is a `LIST` of `STRING` values formatted as `'index: value'`.
462
+
463
+
.Map list elements using indexes
464
+
[source, cypher]
465
+
----
466
+
WITH [1,2,3,4] AS list
467
+
RETURN [listIndex IN range(0, size(list)-1) | toString(listIndex) || ': ' || toString(list[listIndex])] AS mappedListElements
The below query iterates over the `skills` property of each `Person` node and creates a new `LIST` by xref:expressions/string-operators.adoc[concatenating the `STRING`] `" expert"` to each element in `skills`.
392
481
393
482
.Modify `LIST` properties using list comprehension
394
483
[source, cypher]
@@ -423,7 +512,6 @@ MATCH (p:Person)
423
512
RETURN [person IN collect(p) WHERE 'Python' IN person.skills | person.name] AS pythonExperts
0 commit comments