{"id":24568,"date":"2021-10-22T09:00:08","date_gmt":"2021-10-22T03:30:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=24568"},"modified":"2021-11-05T19:38:55","modified_gmt":"2021-11-05T14:08:55","slug":"python-reversed-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-reversed-function-with-examples\/","title":{"rendered":"Python reversed() Function with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python Program for chr() Function<\/a>
\nreversed() Function in Python:<\/strong><\/p>\n

The reversed() function returns an iterator object that has been reversed.<\/p>\n

Syntax:<\/strong><\/p>\n

reversed(sequence)<\/pre>\n

Parameters<\/strong><\/p>\n

sequence:<\/strong> This is required.Any object that is iterable. (list,tuple..etc)<\/p>\n

A sequence is an object that implements the sequence protocols __len__() and __getitem__(). For instance, tuple, string, list, range, and so on.<\/p>\n

In addition, we can use reversed() in any object that implements __reverse__ ().<\/p>\n

Return Value:<\/strong><\/p>\n

The reversed() function returns an iterator that iterates through the given sequence in reverse order.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given string = 'Python-programs'\r\nGiven tuple = (\"h\", \"e\", \"l\", \"l\", \"o\")<\/pre>\n

Output:<\/strong><\/p>\n

['s', 'm', 'a', 'r', 'g', 'o', 'r', 'p', '-', 'n', 'o', 'h', 't', 'y', 'P']\r\n['o', 'l', 'l', 'e', 'h']<\/pre>\n

Note: <\/strong>After applying the reverse function the result is converted into the list.Hence the result is in the list form.<\/p>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given list =  [6, 7, 8, 9, 10]\r\nGiven range = (2, 12)<\/pre>\n

Output:<\/strong><\/p>\n

[10, 9, 8, 7, 6]\r\n[11, 10, 9, 8, 7, 6, 5, 4, 3, 2]<\/pre>\n

Program for reversed() Function in Python<\/h2>\n