Member-only story
Hi everyone, welcome back. Today, we will be going over the reversed() function in Python and see how it works. The reversed() function is a built in function and can be used to create a new iterable object with it’s elements in reverse order. With this introduction out of the way, let’s get into it.
Reversed() Function
Let’s see how the reversed() function works:
list = [1, 2, 3]
result = reversed(list)
for i in result:
print(i)Output:
3
2
1
As we can see in the example above, we created a list with a few values and reversed it. We then looped through the reversed list and printed each individual value. Our output displays the values in reversed order, which is expected.
Conclusion
This is it for the reversed() function. It’s a straight forward function that returns a new iterable object with elements in reversed order. I hope this helps. Thanks for reading.