๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_howto_reverse_string.asp
How to reverse a String in Python
There is no built-in function to reverse a String in Python. The fastest (and easiest?) way is to use a slice that steps backwards, -1. ... Create a slice that starts at the end of the string, and moves backwards.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ reversed
Python reversed()
country_capitals = { 'England': 'London', 'Canada': 'Ottawa', 'Germany': 'Berlin' } # access keys in reversed order # and convert it to a tuple country = tuple(reversed(country_capitals)) print(country)
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_reversed.asp
Python reversed() Function
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary ยท Built-in Modules Random Module Requests Module Statistics Module Math Module cMath Module ยท Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_reverse.asp
Python List reverse() Method
The built-in function reversed() returns a reversed iterator object. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-reversed-function
Python reversed() Method - GeeksforGeeks
January 8, 2018 - It works with sequence types like lists, tuples, strings and ranges or any object that implements the __reversed__() method. Example: In this example, a list is reversed using reversed() and converted into a list for display.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ builtin-functions โ€บ reversed
reversed() | Pythonโ€™s Built-in Functions โ€“ Real Python
The function does not modify the original iterable: ... An iterator that yields the elements of the input iterable in reverse order.
๐ŸŒ
Real Python
realpython.com โ€บ reverse-string-python
Reverse Strings in Python: reversed(), Slicing, and More โ€“ Real Python
July 31, 2023 - Passing None to the first two arguments of slice() tells the function that you want to rely on its internal default behavior, which is the same as a standard slicing with no values for start and stop. In other words, passing None to start and stop means that you want a slice from the left end to the right end of the underlying sequence. ... The second and arguably the most Pythonic approach to reversing strings is to use reversed() along with str.join().
๐ŸŒ
ThePythonGuru
thepythonguru.com โ€บ python-builtin-functions โ€บ reversed
Python reversed() function - ThePythonGuru.com
Its syntax is as follows: Syntax: reversed(sequence) -> reverse iterator ยท Here are some examples: Try it out: print( reversed([44, 11, -90, 55, 3]) ) print(list(reversed([44, 11, -90, 55, 3]))) # reversing a list print( list(reversed((6, 1, 3, 9)))) # reversing a tuple print(list(reverse...
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ python โ€บ python reversed() function
Python reversed() Function Explained
February 21, 2009 - Learn how to use the Python reversed() function to reverse sequences. Understand its syntax, parameters, and practical examples.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-reverse
Python List Reverse() - GeeksforGeeks
April 25, 2025 - In this example, we are reversing the list of mixed data types using reverse() method. ... Another common way to reverse a list in Python is using slicing ([::-1]).
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ built-in functions โ€บ reversed()
Python | Built-in Functions | reversed() | Codecademy
August 22, 2023 - Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today. ... Because the reversed() function returns an iterator object, in order to access the content, it is necessary to iterate over the object when printing: ... The example below reverses the elements of a list.
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ reverse
Python reverse - reversing Python sequences
In the function, we use a while loop to build the new string in reverse order. The __reversed__ magic method implementation should return a new iterator object that iterates over all the objects in the container in reverse order. ... #!/usr/bin/python class Vowels: def __init__(self): self.vowels ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python reversed() function with examples
Python reversed() Function with Examples - Spark By {Examples}
May 31, 2024 - The reversed() is a built-in function ... back to the iterable object you want. For example, if you want the result in the list, use the list(list_reverseiterator)....
Top answer
1 of 2
3

No, this is not possible in Python (or most/any other languages). For any function that does slightly complicated branching or even common math operations, it is completely impossible to undo. One very simple example:

def foo(a, b):
    return a + b

foo.undo(6)  # made-up example syntax

What should this return? 0 and 6, or maybe -13 and 19? A reversible function needs to have an unambiguous mapping from both input to output and output to input. Anything in the function that would cause two different inputs to create the same output will break this mapping.

Especially in your example, you are utilizing randomness. How could the program even know what to undo if it is randomized? The mapping isn't even consistent for input to output, much less the other way around.

If you want to do something like this, you could use simple substitutions only (such as rot13), which provides a direct mapping between characters. You could also keep track of previous values in a dict with the function results being the keys, which would work because it is creating the mapping as it goes. This obviously would not work on its own across multiple runs of the program though, as it would not preserve the mapping.

Whichever method you choose, you will definitely need to write your own undo function.

2 of 2
1

Many answers have pointed out how what you're describing (reversing the actions of a generic function) is mathematically impossible. I'll show you here a way you could accomplish this under some very specific circumstances, though I will hasten to point out that they are correct -- this will not work in the general case.

However, if you're frequently round-tripping these results, it might be helpful to memoize the results of your hash function and do a reverse lookup.

# This code assumes that the memo dictionary need not be bounded in size.
# A real implementation will likely include a method to cull old results
# once the memo reaches a certain size. See `functools.lru_cache` for a
# specific example of this made for speeding up repeated function calls.

_memo_dict = dict()

def hash(p):
    # produces strlist as above, then...

    _key = tuple(strlist)
    _memo_dict[_key] = p

def unhash(hashed_p: list[int]) -> str:
    cache_hit = _memo_dict.get(tuple(hashed_p))
    if cache_hit is not None:
        # we've previously hashed a string to get this
        # value so we can skip the calculations to reverse
        # the process and just hand back the result
        return cache_hit

    # otherwise, you should have some way to reverse it
    # manually here.
๐ŸŒ
Python Academy
python-academy.org โ€บ functions โ€บ reverse
reverse โ€” Python Function Reference
A comprehensive guide to Python functions, with examples. Find out how the reverse function works in Python. Reverse the elements of the list in place.
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ reversed-in-python
Reversed in Python | Python reversed() Function - Scaler Topics
March 2, 2022 - In this example, we reversed a list with three elements and then accessed each element with the next function after accessing the 1, the last element in the reversed list. The iterator gets exhausted.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ how-to-reverse-a-list-in-python
How to Reverse a List in Python | Codecademy
Learn how to reverse a list in Python using `.reverse()`, `reversed()`, slicing, the two-pointer method, loops, and recursion with examples.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ list-reverse-method
Python List reverse() Method: Syntax, Methods, and Examples
Learn how to reverse a list in Python using slicing, reverse(), reversed(), and loops. Compare methods, use cases, and performance best practices.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ what is python reversed() function?
What is Python reversed() function? - AskPython
August 6, 2022 - Now, let us understand the implementation of Python reversed() function in the below section. In the below example, we have created a list of numeric data values and passed it to the reversed() function.