{"id":927,"date":"2022-12-03T13:27:00","date_gmt":"2022-12-03T07:57:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=927"},"modified":"2023-08-26T10:50:46","modified_gmt":"2023-08-26T05:20:46","slug":"different-ways-to-reverse-a-python-list","status":"publish","type":"post","link":"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list","title":{"rendered":"8 Different Ways To Reverse A Python List"},"content":{"rendered":"\n<p>Lists are one of the in-built data types in Python and fall into the sequence category. That means lists are used for working with the sequence of objects and we can say a sequence is a Python list when they are wrapped within (<code>[ ]<\/code>) square brackets.<\/p>\n\n\n\n<p>Just like other data types, lists have numerous methods and function which helps us in modifying and manipulating the elements inside the list.<\/p>\n\n\n\n<p>Python list has a function named&nbsp;<code>reverse()<\/code>&nbsp;which is used to&nbsp;<strong>reverse the elements of the list<\/strong>.<\/p>\n\n\n\n<p>But in this article, you\u2019ll see the various ways to reverse the elements specified inside the list. The&nbsp;<code>list.reverse()<\/code>&nbsp;function is one you might know but you\u2019ll also see other ways to perform the same operation.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-listreverse\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-listreverse\"><\/a><strong>Using list.reverse()<\/strong><\/h1>\n\n\n\n<p>Python&nbsp;<code>list.reverse()<\/code>&nbsp;used to&nbsp;<strong>reverse the elements specified inside the list<\/strong>. If you notice that it is&nbsp;<code>list.reverse<\/code>, this function is specific to Python lists and cannot be used for other data types.<\/p>\n\n\n\n<p>In the following example, a list is defined and stored in the&nbsp;<code>my_lst<\/code>&nbsp;variable and then performed the reverse operation. This will reverse the order of the elements.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\n# Using reverse function\nmy_lst.reverse()\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 \">['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<p><strong>Note<\/strong>: If you try to reverse any data type other than lists, an&nbsp;<code>AttributeError<\/code>&nbsp;will be raised stating that&nbsp;<strong>\u2018x\u2019 has no attribute \u2018reverse\u2019<\/strong>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-reversed\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-reversed\"><\/a><strong>Using reversed()<\/strong><\/h1>\n\n\n\n<p>Python&nbsp;<code>reversed()<\/code>&nbsp;function is also used to&nbsp;<strong>reverse the elements inside the list. Still, instead of returning the reversed object, it returns the reversed iterator object that accesses the values of a given sequence<\/strong>. To access the values, you have to iterate them either by using the&nbsp;<code>for<\/code>&nbsp;loop or the&nbsp;<code>next()<\/code>&nbsp;function.<\/p>\n\n\n\n<p>Consider the following example, the list object&nbsp;<code>my_lst<\/code>&nbsp;is reversed and then accessed the values using the&nbsp;<code>next()<\/code>&nbsp;function from the reversed iterator object&nbsp;<code>new_lst<\/code>.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\nnew_lst = reversed(my_lst)\n# Iterating over the reversed iterator obj\nprint(next(new_lst))\nprint(next(new_lst))\nprint(next(new_lst))\nprint(next(new_lst))\nprint(next(new_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 \">u\no\ni\ne\na<\/pre><\/div>\n\n\n\n<p>One main thing is Python&nbsp;<code>reversed()<\/code>&nbsp;isn\u2019t like the&nbsp;<code>list.reverse()<\/code>&nbsp;function because it can be used for any iterable sequence.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-list-comprehension\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-list-comprehension\"><\/a><strong>Using list comprehension<\/strong><\/h1>\n\n\n\n<p>Using list comprehension for reversing the list is different from the approach you\u2019ve seen in the above two ways.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\n# Taking the index value\nindex = len(my_lst) - 1\n\n# Using list comprehension\nrev_list = [my_lst[i] for i in range(index, -1, -1)]\nprint(rev_list)<\/pre><\/div>\n\n\n\n<p>In the above Python program, a list is defined and then the variable&nbsp;<code>index<\/code>&nbsp;is defined that stores the list\u2019s index value by taking the list\u2019s length minus 1. Then created a&nbsp;<strong>list comprehension<\/strong>&nbsp;<strong>that accesses each index item of the list using the range function from the<\/strong>&nbsp;<code>index<\/code>&nbsp;<strong>to the end of the list in reverse order<\/strong>.<\/p>\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 \">['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-for-loops\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-for-loops\"><\/a><strong>Using for loops<\/strong><\/h1>\n\n\n\n<p>Python&nbsp;<code>for<\/code>&nbsp;loop is a great way to iterate over a sequence. Let\u2019s see how to reverse a list using the&nbsp;<code>for<\/code>&nbsp;loop.<\/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 = ['a', 'e', 'i', 'o', 'u']\n# Defining an empty list\nrev_list = list()\n\n# Iterating each element from the original list\nfor elem in my_lst:\n    rev_list = [elem] + rev_list\n\nprint(rev_list)<\/pre><\/div>\n\n\n\n<p>First, we defined two lists, one is our original list (<code>my_lst<\/code>) and the other is the empty list (<code>rev_list<\/code>) to store the reversed elements.<\/p>\n\n\n\n<p>Then we iterated over each element from the original list and added them to our empty list named&nbsp;<code>rev_list<\/code>. The output will be a reversed version of the original list.<\/p>\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 \">['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<p>If you are wondering how it happened, then look at the example below<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">for elem in my_lst:\n    rev_list = [elem] + rev_list\n    print(rev_list)<\/pre><\/div>\n\n\n\n<p>If we print&nbsp;<code>rev_list<\/code>&nbsp;each time the elements from the original list are added then at the end we\u2019ll get the reversed list.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['a']\n['e', 'a']\n['i', 'e', 'a']\n['o', 'i', 'e', 'a']\n['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-reverse-list-indexing\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-reverse-list-indexing\"><\/a><strong>Using reverse list indexing<\/strong><\/h1>\n\n\n\n<p>The elements in Python lists have an index value that helps access the specific element. The index value starts with&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>. List indexing can be done using the format&nbsp;<code>[start : stop: step]<\/code>.<\/p>\n\n\n\n<p>For example,&nbsp;<code>[: : 1]<\/code>&nbsp;will return the whole list from start to end. Similarly,&nbsp;<code>[: : -1]<\/code>&nbsp;will return the whole list but in reversed order. Let\u2019s see an example.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\nnew_list = my_lst[: : -1]\nprint(new_list)<\/pre><\/div>\n\n\n\n<p>The above code will step over the elements from the original list in reverse order.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-slice-method\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-slice-method\"><\/a><strong>Using slice method<\/strong><\/h1>\n\n\n\n<p>Python has a function named&nbsp;<code>slice()<\/code>&nbsp;that returns a slice object, which specifies how to slice a sequence. It takes 3 arguments (<code>start, stop, step<\/code>).&nbsp;<code>start<\/code>&nbsp;that specifies from where to start slicing,&nbsp;<code>stop<\/code>&nbsp;that specifies where to end and&nbsp;<code>step<\/code>&nbsp;that specifies the step of the slicing.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\n# Using the slice method\nreverse = slice(None, None, -1)\n\nnew_list = my_lst[reverse]\nprint(new_list)<\/pre><\/div>\n\n\n\n<p>In the above program, created a slice object that reverses the order of the original list and is then used to reverse the original list.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">['u', 'o', 'i', 'e', 'a']<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-range-function\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-range-function\"><\/a><strong>Using range function<\/strong><\/h1>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \"># Defining a list\nmy_lst = ['a', 'e', 'i', 'o', 'u']\n\n# Accessing the last index value\nindex = len(my_lst)-1\n\n# Using range function\nfor elem in range(index, -1, -1):\n    print(my_lst[elem], end=\" \")<\/pre><\/div>\n\n\n\n<p>In the above Python program, we are using a&nbsp;<code>range()<\/code>&nbsp;function that is grabbing the last index value of the item and going all the way to the end in the reverse order. It is just like what we\u2019ve done using list comprehension.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">u o i e a<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-using-reversed-1\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-using-reversed-1\"><\/a><strong>Using __reversed__()<\/strong><\/h1>\n\n\n\n<p>Python list has a special method called&nbsp;<code>__reversed__()<\/code>&nbsp;that helps in reverse iteration. If you remember we saw the&nbsp;<code>reversed()<\/code>&nbsp;function, basically it runs the&nbsp;<code>__reversed__()<\/code>&nbsp;method in the backend to reverse the input list.<\/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 = ['a', 'e', 'i', 'o', 'u']\n\nnew_list = my_lst.__reversed__()\n\n# Iterating over the reversed object\nfor elem in new_list:\n    print(elem, end=\" \")<\/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 \">u o i e a<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/different-ways-to-reverse-a-python-list#heading-conclusion\"><\/a><strong>Conclusion<\/strong><\/h1>\n\n\n\n<p>You\u2019ve learned the various ways that you can use to reverse the Python lists. Some of the ways you might already know and some don\u2019t.<\/p>\n\n\n\n<p>Let\u2019s recall the methods you\u2019ve learned in this article to reverse the list:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python list&nbsp;<code>reverse()<\/code>&nbsp;function<\/li>\n\n\n\n<li>Python&nbsp;<code>reversed()<\/code>&nbsp;function<\/li>\n\n\n\n<li>Using list comprehension<\/li>\n\n\n\n<li>Using&nbsp;<code>for<\/code>&nbsp;loop<\/li>\n\n\n\n<li>Reverse list indexing<\/li>\n\n\n\n<li>Python&nbsp;<code>slice()<\/code>&nbsp;method<\/li>\n\n\n\n<li>Using&nbsp;<code>range()<\/code>&nbsp;function<\/li>\n\n\n\n<li>Using&nbsp;<code>__reversed__()<\/code>&nbsp;method<\/li>\n<\/ul>\n\n\n\n<p>Now, it\u2019s your choice which method you should use but most developers go for the easy method and prefer using the&nbsp;<code>list.reverse()<\/code>&nbsp;or&nbsp;<code>reversed()<\/code>&nbsp;function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83c\udfc6<strong>Other articles you might like<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/4-ways-of-string-formatting-in-python-guide\" rel=\"noreferrer noopener\">4 ways of string formatting in Python<\/a><strong>.<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/how-to-open-and-read-multiple-files-using-with-in-python\" rel=\"noreferrer noopener\">Open and read multiple files simultaneously in Python<\/a><strong>.<\/strong><\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/multiple-inputs-in-python\" rel=\"noreferrer noopener\">Take multiple inputs from the user in a single line in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/ways-to-remove-whitespaces-from-the-string-in-python-with-examples-beginners-guide\" rel=\"noreferrer noopener\">Ways to remove whitespace from the string in Python<\/a>.<\/p>\n\n\n\n<p>\u2705<a target=\"_blank\" href=\"https:\/\/geekpython.in\/python-bitwise-operators-with-examples-explained-in-detail\" rel=\"noreferrer noopener\">Python Bitwise operator and what happens behind the scene<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That\u2019s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lists are one of the in-built data types in Python and fall into the sequence category. That means lists are used for working with the sequence of objects and we can say a sequence is a Python list when they are wrapped within ([ ]) square brackets. Just like other data types, lists have numerous [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":930,"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,7],"tags":[12,31],"class_list":["post-927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-python-tips","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/927","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=927"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":1337,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/927\/revisions\/1337"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}