Reversing a List in Python: A Step-by-Step Tutorial

Is it true or not that you are jumping further into Python records and needing to find out with regards to various ways of turning around them? Assuming this is the case, then, at that point, this instructional exercise is for you. Here, you’ll find out about a couple of Python instruments and strategies that are convenient with regards to switching records or controlling them in invert requests. This information will supplement and further develop your rundown related abilities and make you more capable with them.

Python furnishes us with different methods of switching a rundown. We will go through not many of the numerous methods on how a rundown in python can be switched.

Examples:

Input : list = [10, 11, 12, 13, 14, 15]
Output : [15, 14, 13, 12, 11, 10]

Input : list = [4, 5, 6, 7, 8, 9]
Output : [9, 8, 7, 6, 5, 4]

Reversing a List in Python: A Step-by-Step Tutorial

Using the switched() worked in work.

In this strategy, we neither oppose a rundown in-place(modify the first rundown), nor we make any duplicate of the rundown. All things considered, we get a converse iterator which we use to burn through the rundown.

# Reversing a list using reversed()
def Reverse(lst):
    return [ele for ele in reversed(lst)]
     
# Driver Code
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

Output:

[15, 14, 13, 12, 11, 10]

Using the opposite() worked in work.

Utilizing the converse() technique we can switch the substance of the rundown object set up i.e., we don’t have to make another rundown rather we simply duplicate the current components to the first rundown in turn around request. This technique straightforwardly adjusts the first rundown.

# Reversing a list using reverse()
def Reverse(lst):
    lst.reverse()
    return lst
     
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

Output:

[15, 14, 13, 12, 11, 10]

Using the cutting method.

In this method, a duplicate of the rundown is made and the rundown isn’t arranged set up. Making a duplicate requires more space to hold the entirety of the current components. This debilitates more memory.

# Reversing a list using slicing technique
def Reverse(lst):
    new_lst = lst[::-1]
    return new_lst
     
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))

Output:

[15, 14, 13, 12, 11, 10]

Example 1: Reverse a List

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)
# List Reverse systems.reverse()


# updated list
print('Updated List:', systems)

Output

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

There are other several ways to reverse a list.


Example 2: Reverse a List Using Slicing Operator

# Operating System List
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

# Reversing a list	
# Syntax: reversed_list = systems[start:stop:step] reversed_list = systems[::-1]


# updated list
print('Updated List:', reversed_list)

Output

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

Example 3: Accessing Elements in Reversed Order

If you need to access individual elements of a list in the reverse order, it’s better to use the reversed() function.

# Operating System List
systems = ['Windows', 'macOS', 'Linux']

# Printing Elements in Reversed Order
for o in reversed(systems): print(o)
Output
Linux
macOS
Windows

Also ReadHow to Split a String in Golang?

Leave a Reply

Your email address will not be published.