Reference · Lists
reverse
signature
reverse(list | str) -> list | strWhat 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
- An empty list gives
[]and the empty string gives"". - A string is reversed by character, not by byte, so accented letters and emoji stay whole.
- Only a list or a string: unlike most list functions,
nullis not taken as an empty list (see Errors). - The list given is not changed;
reversebuilds a new one.
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 nullthis one fails
print(reverse(42))output
line 1: `reverse` expects a list or str, got intChaining
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]OLLEHcbaSee also
sort, sort_by, first, last, slice
Thread
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