{"id":903,"date":"2022-11-21T22:53:00","date_gmt":"2022-11-21T17:23:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=903"},"modified":"2024-01-17T21:15:25","modified_gmt":"2024-01-17T15:45:25","slug":"reverse-vs-reversed-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/reverse-vs-reversed-in-python","title":{"rendered":"Reverse Vs Reversed Function In Python &#8211; Comparison"},"content":{"rendered":"\n<p><strong>Lists<\/strong>&nbsp;are one of the in-built data types in Python used to store the collections of data. It is used to work with a sequence of data. Besides data types, Python has numerous standard functions for performing specific tasks.<\/p>\n\n\n\n<p>Python lists are probably the first data type that most Python beginners start learning in a sequence category, and along with this, their functions and methods. The&nbsp;<code>reverse()<\/code>&nbsp;function is one of the functions of Python Lists&nbsp;<strong>used to reverse the elements from the list<\/strong>.<\/p>\n\n\n\n<p>To get the same work done, Python has a function named&nbsp;<code>reversed()<\/code>, which is also&nbsp;<strong>used to reverse the sequence of data<\/strong>.<\/p>\n\n\n\n<p>In this article, we will deep dive into both functions and understand their fundamentals and usage along with examples, and then at the end of this article, we&#8217;ll point out the differences between them.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<p class=\"responsive-video-wrap clr\"><iframe loading=\"lazy\" title=\"Python&#039;s reverse() Vs reversed() | 2MinutesPy\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/bchi-TI5Uy8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><\/p>\n<\/div><figcaption class=\"wp-element-caption\">Summary Video<\/figcaption><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-list-reverse-function\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-list-reverse-function\"><\/a>List reverse() function<\/h1>\n\n\n\n<p>As we discussed earlier, the&nbsp;<code>reverse()<\/code>&nbsp;function from Python List is used to&nbsp;<strong>reverse the elements in a List<\/strong>. This function does what its name depicts itself.<\/p>\n\n\n\n<p>The list&nbsp;<code>reverse()<\/code>&nbsp;function&nbsp;<strong>operates on the original list<\/strong>. It reverses the list and then returns it. If we try to print the original list, we&#8217;ll get the reversed list because the original list no longer exists, and the reversed list is the original.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">my_lst = [2, 4, 3, 67, 5, 3]\nmy_lst.reverse()\n\nprint(my_lst)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">[3, 5, 67, 3, 4, 2]<\/pre><\/div>\n\n\n\n<p>We tried to print the original list, but due to the use of the&nbsp;<strong><em>reverse()<\/em><\/strong>&nbsp;function, our list got modified, and we got the reversed list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-syntax-reverse\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-syntax-reverse\"><\/a>Syntax &#8211; reverse()<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>list.reverse()<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p><strong>Parameter<\/strong><\/p>\n\n\n\n<p>There are no parameters, and neither it takes any arguments.<\/p>\n\n\n\n<p><strong>Return Value<\/strong><\/p>\n\n\n\n<p>It does not return any value but reverses the elements from the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-usage-reverse\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-usage-reverse\"><\/a>Usage &#8211; reverse()<\/h2>\n\n\n\n<p>We have two lists in the following example, the first list has collections of integers, and the second contains multiple data types.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># List of numbers\nlst1 = [2, 4, 3, 67, 5, 3]\nlst1.reverse()\nprint('First List: ', lst1)\n\n# List containing multiple data types\nlst2 = ['a', 2, 'w', 'u', 3j+1]\nlst2.reverse()\nprint('Second List: ', lst2)<\/pre><\/div>\n\n\n\n<p>We used the&nbsp;<strong><em>reverse()<\/em><\/strong>&nbsp;function on both lists and got the output as we expected it should be.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">First List:  [3, 5, 67, 3, 4, 2]\nSecond List:  [(1+3j), 'u', 'w', 2, 'a']<\/pre><\/div>\n\n\n\n<p>We put the&nbsp;<strong><em>reverse()<\/em><\/strong>&nbsp;function to practical use in the following example when we check whether a specified value is a palindrome.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Creating a list from a string\nlst = list('malayalam')\n\n# Creating a copy of the original list\nnew_lst = lst.copy()\n# Reversing\nnew_lst.reverse()\n\n# Comparing the lists\nif lst == new_lst:\n    print(\"It's a Palindrome.\")\nelse:\n    print(\"Not a Palindrome.\")<\/pre><\/div>\n\n\n\n<p>The value&nbsp;<strong>&#8220;malayalam&#8221;<\/strong>&nbsp;is a palindrome because it will remain the same whether we reverse it or not. That&#8217;s why we got the output&nbsp;<strong>&#8220;It&#8217;s a Palindrome&#8221;<\/strong>&nbsp;because the original and reversed lists are the same.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">It's a Palindrome.<\/pre><\/div>\n\n\n\n<p><strong>What will happen if we try to reverse a string?<\/strong>&nbsp;In the above example, we specified the string but converted it into the list before performing the reverse operation.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defining a String\nmy_str = \"geekpython\"\n# Trying to reverse a string\nmy_str.reverse()\n\nprint(my_str)<\/pre><\/div>\n\n\n\n<p>We&#8217;ll get an error if we use the&nbsp;<code>reverse()<\/code>&nbsp;function on a string.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Traceback (most recent call last):\n  File \"D:\\SACHIN\\Pycharm\\reverse_vs_reversed\\main.py\", line 28, in &lt;module&gt;\n    my_str.reverse()\nAttributeError: 'str' object has no attribute 'reverse'<\/pre><\/div>\n\n\n\n<p>The error says that the&nbsp;<strong><em>string<\/em><\/strong>&nbsp;has no attribute reverse. We cannot use the reverse function on the&nbsp;<strong><em>string<\/em><\/strong>&nbsp;data type or any other data type. The&nbsp;<code>reverse()<\/code>&nbsp;function can only be used for the&nbsp;<strong><em>list<\/em><\/strong>&nbsp;data type.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-python-reversed-function\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-python-reversed-function\"><\/a>Python reversed() function<\/h1>\n\n\n\n<p>Python&nbsp;<code>reversed()<\/code>&nbsp;function takes a sequence and returns the reversed iterator object. It is the same as the&nbsp;<code>iter()<\/code>&nbsp;method but in reverse order. We can access the reversed objects either by iterating them or using the&nbsp;<code>next()<\/code>&nbsp;function.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defining a list\nmy_lst = ['geeks', 'there', 'Hey']\n\n# Calling the reversed() function on the original list\nnew_lst = reversed(my_lst)\n# Accessing the reversed objects one by one\nprint(next(new_lst))\nprint(next(new_lst))\nprint(next(new_lst))<\/pre><\/div>\n\n\n\n<p>We used the&nbsp;<code>next()<\/code>&nbsp;function to access the iterator objects.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Hey\nthere\ngeeks<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-syntax-reversed\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-syntax-reversed\"><\/a>Syntax &#8211; reversed()<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>reversed(sequence)<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p><strong>Parameter<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sequence<\/strong>&nbsp;&#8211; Any sequence object that is iterable.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Value<\/strong><\/p>\n\n\n\n<p>Returns the reversed iterator object of the given sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-usage-reversed\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-usage-reversed\"><\/a>Usage &#8211; reversed()<\/h2>\n\n\n\n<p>We have already seen the example where we performed the reverse operation on the Python&nbsp;<strong><em>list<\/em><\/strong>&nbsp;using the&nbsp;<code>reversed()<\/code>&nbsp;function. This time we&#8217;ll try to reverse the Python&nbsp;<strong><em>string<\/em><\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">my_str = \"nohtyPkeeG morf sgniteerG\"\n\nfor rev_str in reversed(my_str):\n    print(rev_str, end=\"\")<\/pre><\/div>\n\n\n\n<p>We used the&nbsp;<code>for<\/code>&nbsp;loop to iterate over the reversed sequence and then printed it in a single line.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Greetings from GeekPython<\/pre><\/div>\n\n\n\n<p>Similarly, we can use any sequence object that is iterable.&nbsp;<strong><em>List<\/em><\/strong>,&nbsp;<strong><em>Tuple<\/em><\/strong>, and&nbsp;<strong><em>Dictionary<\/em><\/strong>&nbsp;objects are iterable and can be reversed. The&nbsp;<strong><em>Set<\/em><\/strong>&nbsp;objects can also be iterated but they are not&nbsp;<strong>reversible<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defining a Tuple\nmy_tup = (\"GeekPython\", \"from\", \"Greetings\")\n# Defining a Dictionary\nmy_dict = {\"a\": 1, \"b\": 0, \"c\": 7}\n\n# Reversing a Tuple\nfor rev_tup in reversed(my_tup):\n    print(rev_tup, end=\" \")\nprint(\"\\n\")\n\n# Reversing a Dictionary\nfor rev_dict in reversed(my_dict):\n    print(rev_dict, end=\" \")<\/pre><\/div>\n\n\n\n<p>The output will be a reversed tuple and keys of a dictionary.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Greetings from GeekPython \n\nc b a<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-whats-the-difference\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-whats-the-difference\"><\/a>What&#8217;s the difference?<\/h1>\n\n\n\n<p>We&#8217;ve seen the definition and usage of both functions and are now able to differentiate between them. The primary work of both functions is to reverse the objects of the given sequence. But there are some key differences between the&nbsp;<code>reverse()<\/code>&nbsp;and&nbsp;<code>reversed()<\/code>&nbsp;functions which are listed below.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\"><code>reverse()<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\"><code>reversed()<\/code><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">The&nbsp;<code>reverse()<\/code>&nbsp;function can only reverse the Python&nbsp;<strong><em>list<\/em><\/strong>.<\/td><td class=\"has-text-align-left\" data-align=\"left\">The&nbsp;<code>reversed()<\/code>&nbsp;function can be used to reverse&nbsp;<strong><em>lists<\/em><\/strong>,&nbsp;<strong><em>tuples<\/em><\/strong>,&nbsp;<strong><em>strings<\/em><\/strong>, <strong><em>dictionaries<\/em><\/strong>,&nbsp;or any sequence that is iterable.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Returns the&nbsp;<strong>reversed sequence<\/strong>.<\/td><td class=\"has-text-align-left\" data-align=\"left\">Returns the&nbsp;<strong>reversed iterator object of the sequence<\/strong>.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Operates on the&nbsp;<strong>original list<\/strong>&nbsp;and&nbsp;<strong>modifies<\/strong>&nbsp;it.<\/td><td class=\"has-text-align-left\" data-align=\"left\">Creates the&nbsp;<strong>iterator object<\/strong>&nbsp;from the&nbsp;<strong>original sequence<\/strong>&nbsp;and&nbsp;<strong>the original sequence remains the same<\/strong>.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Takes no&nbsp;<strong>arguments<\/strong>.<\/td><td class=\"has-text-align-left\" data-align=\"left\">Any&nbsp;<strong>sequence<\/strong>&nbsp;that is&nbsp;<strong>iterable is required<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/reverse-vs-reversed-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>This article covered the fundamentals and usage of the&nbsp;<code>reverse()<\/code>&nbsp;and&nbsp;<code>reversed()<\/code>&nbsp;functions and we coded some examples along with them. Through this article, we meant to dive into the working and usage of the functions and of course, point out the key differences between them.<\/p>\n\n\n\n<p>Let&#8217;s review what we&#8217;ve learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Usage of list&nbsp;<code>reverse()<\/code>&nbsp;function<\/strong><\/li>\n\n\n\n<li><strong>Usage of the Python&nbsp;<code>reversed()<\/code>&nbsp;function<\/strong><\/li>\n\n\n\n<li><strong>Key differences between both functions<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lists&nbsp;are one of the in-built data types in Python used to store the collections of data. It is used to work with a sequence of data. Besides data types, Python has numerous standard functions for performing specific tasks. Python lists are probably the first data type that most Python beginners start learning in a sequence [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":906,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,41],"tags":[12,31],"class_list":["post-903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-function","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=903"}],"version-history":[{"count":8,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":1613,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/903\/revisions\/1613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/906"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}