Reference · Lists

reverse

signature
reverse(list | str) -> list | str

What it does

A list with its items in the opposite order, or a string with its characters in the opposite order. The value given is left as it was.

Examples

print(reverse([1, 2, 3]))print(reverse("hello"))print(reverse(sort([3, 1, 2])))
output
[3, 2, 1]olleh[3, 2, 1]

Edge cases

print(reverse([]), len(reverse("")))print(reverse("añb"), reverse("🙂ab"))let xs = [1, 2, 3]let r = reverse(xs)print(xs, r)
output
[] 0bña ba🙂[1, 2, 3] [3, 2, 1]

Errors

An argument that is not a list or a string is a runtime error, not an error value, and that includes null. Give an empty list with ?? [] first when the value may be null.

this one fails
print(reverse(null))
output
line 1: `reverse` expects a list or str, got null
this one fails
print(reverse(42))
output
line 1: `reverse` expects a list or str, got int

Chaining

In a pipeline reverse takes the value and needs no arguments. It is how a sort is made descending.

print([3, 1, 2] | sort | reverse)print("hello" | reverse | upper)print(["a", "b", "c"] | reverse | join(""))
output
[3, 2, 1]OLLEHcba

See also

sort, sort_by, first, last, slice

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On reverse(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo