Backward iteration in Python Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Backward iteration in Python is traversing a sequence (like list, string etc.) in reverse order, moving from the last element to the first. Python provides various methods for backward iteration, such as using negative indexing or employing built-in functions like reversed().Using reversed() methodUse reversed() method when we simply need to loop backwards without modifying original sequence or there is no need to create a new reversed copy. Python a = [10, 20, 30, 40, 50] #Loop through the list #using reversed() to reverse the order of list for b in reversed(a): print(b) Output50 40 30 20 10 Let's explore more methods that helps to iterate backward on any iterable. Here are some common approaches:Table of ContentUsing range(N, -1, -1)Using List SlicingUsing a While Loop Using range(N, -1, -1)range function is provided with three arguments(N, -1, -1). Use this method when we need to modify elements or access their indices. Python s = "Python" # - len(s) - 1: Start at the last index # - -1: Ensures the loop stops after the first character # - -1: The step value to iterate backwards for i in range(len(s) - 1, -1, -1): print(s[i]) Outputn o h t y P Using Slicing [::-1]We can use slicing slicing notation [::-1] to reverse the order of elements in an iterable. This works for lists, tuples, and strings, but not for sets or dictionaries (since they are unordered). Python a = [1, 2, 3, 4, 5] #Loop through the List # reverse order using slicing(::-1) for item in a[::-1]: print(item) # same logic written using List comprehension # [print(item) for item in a[::-1]] Output5 4 3 2 1 Using a While Loop Using a while loop provides you control over the iteration process. This is need to manually control the index and decrement the index during each iteration to traverse the it backward. Python a = [1, 2, 3, 4, 5] i = len(a) - 1 # Start a while loop that continues # until as 'i' is greater than or equal to 0 while i >= 0: print(a[i]) i -= 1 Output5 4 3 2 1 Comment More infoAdvertise with us Next Article Backward iteration in Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python loop-programs Practice Tags : python Similar Reads Python | Alternate Rear iteration The iteration of numbers is done by looping techniques in python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backwards in alternate way and having shorthands to do so can be quite useful. Letâs discuss certain ways in which this can be d 3 min read Interesting Facts About Python Python is a high-level, general-purpose programming language that is widely used for web development, data analysis, artificial intelligence and more. It was created by Guido van Rossum and first released in 1991. Python's primary focus is on simplicity and readability, making it one of the most acc 7 min read Get first and last elements of a list in Python The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4]. Using indexingThis is the most straightforward way 3 min read Python - Negative index of Element in List We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.Using index()index() method in Python searches for the first occur 3 min read Python program to reverse a range in list Reversing a specific range within a list is a common task in Python programming that can be quite useful, when data needs to be rearranged. In this article, we will explore various approaches to reverse a given range within a list. We'll cover both simple and optimized methods. One of the simplest w 3 min read Like