{"id":1500,"date":"2020-05-04T04:30:57","date_gmt":"2020-05-04T11:30:57","guid":{"rendered":"https:\/\/renanmf.com\/?p=1500"},"modified":"2020-12-29T10:22:47","modified_gmt":"2020-12-29T13:22:47","slug":"reverse-list-python","status":"publish","type":"post","link":"https:\/\/renanmf.com\/reverse-list-python\/","title":{"rendered":"How to reverse a list in Python"},"content":{"rendered":"<p>To reverse a list in-place, that is, without creating a new list, use the builtin <code>reverse()<\/code> method from <code>list<\/code>:<\/p>\n<pre><code class=\"language-python\">car_brands = [&quot;bmw&quot;, &quot;ferrari&quot;, &quot;mclaren&quot;]\n\nprint(car_brands)\n\ncar_brands.reverse()\n\nprint(car_brands)<\/code><\/pre>\n<pre><code>[&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;]\n\n[&#039;mclaren&#039;, &#039;ferrari&#039;, &#039;bmw&#039;]<\/code><\/pre>\n<p>To keep the original list and assign the reversed list to a new one, use the slice syntax:<\/p>\n<pre><code class=\"language-python\">car_brands = [&quot;bmw&quot;, &quot;ferrari&quot;, &quot;mclaren&quot;]\n\ncar_brands_reversed = car_brands[::-1]\n\nprint(car_brands)\n\nprint(car_brands_reversed)<\/code><\/pre>\n<pre><code>[&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;]\n\n[&#039;mclaren&#039;, &#039;ferrari&#039;, &#039;bmw&#039;]<\/code><\/pre>\n<p>The slice syntax allows you to set a step, which is <code>-1<\/code> in the example.<\/p>\n<p>The default step is <code>1<\/code>, that is, go forward 1 item of the list at a time.<\/p>\n<p>If you set the step to <code>-1<\/code> you have the opposite, go back 1 item at a time.<\/p>\n<p>So you start at the position of the last item and move backwards to the first item at position 0.<\/p>\n<p>To learn more about slicing, check out <a href=\"https:\/\/renanmf.com\/slicing-python\/\">Understanding Slicing in Python<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To reverse a list in-place, that is, without creating a new list, use the builtin reverse() method from list: car_brands = [&quot;bmw&quot;, &quot;ferrari&quot;, &quot;mclaren&quot;] print(car_brands) car_brands.reverse() print(car_brands) [&#039;bmw&#039;, &#039;ferrari&#039;, &#039;mclaren&#039;] [&#039;mclaren&#039;, &#039;ferrari&#039;, &#039;bmw&#039;] To keep the original list and assign the reversed list to a new one, use the slice syntax: car_brands = [&quot;bmw&quot;, &quot;ferrari&quot;, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":1502,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6],"class_list":["post-1500","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to reverse a list in Python<\/title>\n<meta name=\"description\" content=\"Learn how to reverse a list in python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/renanmf.com\/reverse-list-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to reverse a list in Python\" \/>\n<meta property=\"og:description\" content=\"Learn how to reverse a list in python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/renanmf.com\/reverse-list-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Renan Moura - Software Engineering\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/renanmouraf\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-04T11:30:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T13:22:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Renan Moura\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/renanmouraf\" \/>\n<meta name=\"twitter:site\" content=\"@renanmouraf\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Renan Moura\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/\"},\"author\":{\"name\":\"Renan Moura\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\"},\"headline\":\"How to reverse a list in Python\",\"datePublished\":\"2020-05-04T11:30:57+00:00\",\"dateModified\":\"2020-12-29T13:22:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/\"},\"wordCount\":117,\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/test.png\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/\",\"name\":\"How to reverse a list in Python\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/test.png\",\"datePublished\":\"2020-05-04T11:30:57+00:00\",\"dateModified\":\"2020-12-29T13:22:47+00:00\",\"description\":\"Learn how to reverse a list in python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/test.png\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/test.png\",\"width\":1200,\"height\":630,\"caption\":\"how to reverse a list in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/reverse-list-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/renanmf.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to reverse a list in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"name\":\"Renan Moura - Software Engineering\",\"description\":\"Software development, machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/renanmf.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\",\"name\":\"Renan Moura\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"width\":120,\"height\":120,\"caption\":\"Renan Moura\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/renanmouraf\",\"https:\\\/\\\/x.com\\\/renanmouraf\",\"https:\\\/\\\/instagram.com\\\/renanmouraf\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\",\"name\":\"Renan Moura\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"caption\":\"Renan Moura\"},\"description\":\"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.\",\"sameAs\":[\"https:\\\/\\\/renanmf.com\\\/\",\"https:\\\/\\\/www.instagram.com\\\/renanmouraf\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/renanmouraf\"],\"url\":\"https:\\\/\\\/renanmf.com\\\/author\\\/renanmoura\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to reverse a list in Python","description":"Learn how to reverse a list in python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/renanmf.com\/reverse-list-python\/","og_locale":"en_US","og_type":"article","og_title":"How to reverse a list in Python","og_description":"Learn how to reverse a list in python.","og_url":"https:\/\/renanmf.com\/reverse-list-python\/","og_site_name":"Renan Moura - Software Engineering","article_publisher":"https:\/\/www.facebook.com\/renanmouraf","article_published_time":"2020-05-04T11:30:57+00:00","article_modified_time":"2020-12-29T13:22:47+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png","type":"image\/png"}],"author":"Renan Moura","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/renanmouraf","twitter_site":"@renanmouraf","twitter_misc":{"Written by":"Renan Moura","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/renanmf.com\/reverse-list-python\/#article","isPartOf":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/"},"author":{"name":"Renan Moura","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8"},"headline":"How to reverse a list in Python","datePublished":"2020-05-04T11:30:57+00:00","dateModified":"2020-12-29T13:22:47+00:00","mainEntityOfPage":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/"},"wordCount":117,"publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"image":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/renanmf.com\/reverse-list-python\/","url":"https:\/\/renanmf.com\/reverse-list-python\/","name":"How to reverse a list in Python","isPartOf":{"@id":"https:\/\/renanmf.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/#primaryimage"},"image":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png","datePublished":"2020-05-04T11:30:57+00:00","dateModified":"2020-12-29T13:22:47+00:00","description":"Learn how to reverse a list in python.","breadcrumb":{"@id":"https:\/\/renanmf.com\/reverse-list-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/renanmf.com\/reverse-list-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/reverse-list-python\/#primaryimage","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/04\/test.png","width":1200,"height":630,"caption":"how to reverse a list in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/renanmf.com\/reverse-list-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/renanmf.com\/"},{"@type":"ListItem","position":2,"name":"How to reverse a list in Python"}]},{"@type":"WebSite","@id":"https:\/\/renanmf.com\/#website","url":"https:\/\/renanmf.com\/","name":"Renan Moura - Software Engineering","description":"Software development, machine learning","publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/renanmf.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/renanmf.com\/#organization","name":"Renan Moura","url":"https:\/\/renanmf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","width":120,"height":120,"caption":"Renan Moura"},"image":{"@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/renanmouraf","https:\/\/x.com\/renanmouraf","https:\/\/instagram.com\/renanmouraf","https:\/\/www.linkedin.com\/in\/renanmouraf\/"]},{"@type":"Person","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8","name":"Renan Moura","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","caption":"Renan Moura"},"description":"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.","sameAs":["https:\/\/renanmf.com\/","https:\/\/www.instagram.com\/renanmouraf\/","https:\/\/www.linkedin.com\/in\/renanmouraf\/","https:\/\/x.com\/https:\/\/twitter.com\/renanmouraf"],"url":"https:\/\/renanmf.com\/author\/renanmoura\/"}]}},"_links":{"self":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/comments?post=1500"}],"version-history":[{"count":14,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1500\/revisions"}],"predecessor-version":[{"id":2614,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/1500\/revisions\/2614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media\/1502"}],"wp:attachment":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media?parent=1500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/categories?post=1500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/tags?post=1500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}