Skip to content

Commit 73b2e80

Browse files
negative indexing for list slicing
1 parent d10b4a9 commit 73b2e80

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

modules/ROOT/pages/expressions/list-expressions.adoc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ RETURN list[2..4] AS middleElements,
207207
3+d|Rows: 1
208208
|===
209209

210+
Negative indexing in list slicing references elements from the end of the `LIST`; `..-1` excludes the last element, `..-2` excludes the last two elements, and so on.
211+
212+
.Negative indexing and list slicing
213+
[source, cypher]
214+
----
215+
WITH [1, 2, 3, 4, 5, 6] AS list
216+
RETURN list[..-1] AS finalElementRemoved,
217+
list[..-2] AS finalTwoElementsRemoved,
218+
list[-3..-1] AS removedFirstThreeAndLast
219+
----
220+
221+
.Result
222+
[role="queryresult",options="header,footer",cols="3*<m"]
223+
|===
224+
| finalElementRemoved | finalTwoElementsRemoved | removedFirstThreeAndLast
225+
226+
| [1, 2, 3, 4, 5] | [1, 2, 3, 4] | [4, 5]
227+
228+
3+d|Rows: 1
229+
|===
230+
210231
When slicing nested `LIST` values, it is important to specify which level is sliced.
211232
The below example slices the outer `LIST` and returns the first two nested `LIST` values.
212233

@@ -326,7 +347,7 @@ RETURN [1, 2] || [3, null] AS listWithNull
326347
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].
327348

328349
[[add-elements]]
329-
=== Add elements to a list
350+
== Add elements to a list
330351

331352
The `+` operator can add elements to the beginning or end of a `LIST` value.
332353
This is not possible using the `||` operator.

0 commit comments

Comments
 (0)