selection.slice(start_offset[, end_offset, left_bound='closed', right_bound='open']) → selection stream.slice(start_offset[, end_offset, left_bound='closed', right_bound='open']) → stream array.slice(start_offset[, end_offset, left_bound='closed', right_bound='open']) → array binary.slice(start_offset[, end_offset, left_bound='closed', right_bound='open']) → binary string.slice(start_offset[, end_offset, left_bound='closed', right_bound='open']) → string
Return the elements of a sequence within the specified range.
slice returns the range between
end_offset. If only
start_offset is specified,
slice returns the range from that index to the end of the sequence. Specify
closed to indicate whether to include that endpoint of the range by default:
closed returns that endpoint, while
open does not. By default,
left_bound is closed and
right_bound is open, so the range
(10,13) will return the tenth, eleventh and twelfth elements in the sequence.
end_offset is past the end of the sequence, all elements from
start_offset to the end of the sequence will be returned. If
start_offset is past the end of the sequence or
end_offset is less than
start_offset, a zero-element sequence will be returned.
end_offset values are allowed with arrays; in that case, the returned range counts back from the array’s end. That is, the range
(-2) returns the last two elements, and the range of
(2,-1) returns the second element through the next-to-last element of the range. An error will be raised on a negative
end_offset with non-arrays. (An
end_offset of −1 is allowed with a stream if
right_bound is closed; this behaves as if no
end_offset was specified.)
slice is used with a binary object, the indexes refer to byte positions within the object. That is, the range
(10,20) will refer to the 10th byte through the 19th byte.
«««< HEAD With a string,
slice behaves similarly, with the indexes referring to Unicode codepoints. String indexes start at
0. (Note that combining codepoints are counted separately.)
If you are only specifying the indexes and not the bounding options, you may use Python’s slice operator as a shorthand:
[start_offset:end_offset]. »»»> master
Example: Return the fourth, fifth and sixth youngest players. (The youngest player is at index 0, so those are elements 3–5.)
Or, using Python’s slice operator:
Example: Return all but the top three players who have a red flag.
Example: Return holders of tickets
Y, assuming tickets are numbered sequentially. We want to include ticket
r.table('users').order_by(index='ticket').slice(x, y, right_bound='closed').run(conn)
Example: Return the elements of an array from the second through two from the end (that is, not including the last two).
Example: Return the third through fifth characters of a string.
> r.expr("rutabaga").slice(2,5).run(conn) "tab"
Couldn't find what you were looking for?
© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.