{"id":3238,"date":"2022-11-12T20:45:18","date_gmt":"2022-11-12T20:45:18","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3238"},"modified":"2022-11-12T20:45:21","modified_gmt":"2022-11-12T20:45:21","slug":"reversing-a-list-in-python","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/reversing-a-list-in-python\/","title":{"rendered":"Reversing A List In Python"},"content":{"rendered":"<p style=\"text-align: center;\">Reversing A List In Python<\/p>\n<p>We have been learning about list slicing, and now, we are going to learn about how we can reversing the list. In order to reverse the list, we simply have to do the list slicing, and we would simply omit the start and stop values, and simply give the step value as -1. Doing this would simply get us the reversed list. Let\u2019s have a look at a simple program, which demonstrates us about reversing the list.<\/p>\n<h2 style=\"text-align: center;\">Reversing A List In Python<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3239 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON.jpg\" alt=\"\" width=\"1002\" height=\"127\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON.jpg 1002w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-300x38.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-768x97.jpg 768w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/p>\n<p>As you can see, in the above program, we have some list, and then we are trying to reverse the list, using list slicing. You can see that we have omitted the start and stop values, and then we have the step value as -1. Doing so will get us the reversed list, assigned to the variable reversed_list.<\/p>\n<p>Also, now Let\u2019s try to provide some invalid stop or a stop which is simply greater than the length of the list, and then Let\u2019s see what do we get. Here is a program, which tries to demonstrate the same thing here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3240 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-1.jpg\" alt=\"\" width=\"1028\" height=\"127\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-1.jpg 1028w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-1-300x37.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-1-1024x127.jpg 1024w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-1-768x95.jpg 768w\" sizes=\"auto, (max-width: 1028px) 100vw, 1028px\" \/><\/p>\n<p>As you can see in the above program, we have a list, and then we are performing list slicing, and we have provided a stop value, which is bigger than the length of the list. In such situations as well, we are not going to get into errors. Instead, we are going to get the list, from the specified start to the end. The output of the above program comes out to be something like this &#8211;<\/p>\n<p>[2, 3, 4, True, &#8216;GyaniPandit&#8217;]<\/p>\n<p>As you can see, we got the list, from the specified start to the end of the list. Similar to this, if we are passing some invalid start to the list, then also we are not going to get into trouble. Instead, if the values that we are providing are incomputable, then in that case, an empty list is created. Have a look at the below example, which gives some demonstration about the same.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3241 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-2.jpg\" alt=\"\" width=\"984\" height=\"105\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-2.jpg 984w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-2-300x32.jpg 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/REVERSING-THE-LIST-IN-PYTHON-2-768x82.jpg 768w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/p>\n<p>As you can see, in the above program, we have a list, and then we are trying to perform list slicing, and we are providing a start, which is already out of range. So, this is some value, which is incomputable, and hence we would end up getting an empty list. This is the output of the above program.<\/p>\n<p>[]<\/p>\n<p>As you can see, we simply get an empty list, when we are having some values, which are simply incomputable.<\/p>\n<p>So, we have explored a lot in relation to accessing the individual list elements, and even we tried to access the range of elements from the list, and for that, we learned list slicing. We can, and we are often going to access the list elements, and also perform list slicing, as and when required in our Python Programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reversing A List In Python We have been learning about list slicing, and now, we are going to learn about how we can reversing the list. In order to reverse the list, we simply have to do the list slicing, and we would simply omit the start and stop values, and simply give the step [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3238","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3238"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3238\/revisions"}],"predecessor-version":[{"id":4061,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3238\/revisions\/4061"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}