Reversing a listing or an array is a typical programming job. There are various circumstances once you may must current information in reverse order, equivalent to when sorting a listing.
How will you reverse a listing or an array with Python? You will study the totally different approaches on this article.
Create a Copy With a for Loop
Whereas Python’s for loop is extra verbose, it is perhaps helpful in some circumstances. As an illustration, it gives extra flexibility when performing complicated logic at some factors within the reverse operation.
When utilizing an indented for loop, the frequent strategy is to iterate by the unique listing in reverse order. Beginning with the ultimate component, every iteration then appends the earlier component to a brand new listing.
Given a listing of integers between one and 9 for example, here is easy methods to reverse an array utilizing an indented for loop:
languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]# Create an empty listing to carry the reversed array:
reversed_list = []
# Substract one from the size of the unique array to start out from the final index:
reducer = len(languages)-1
# Reverse the listing inside a for loop:
for i in languages:
reversed_list.append(languages[reducer]) # Append the outcome to the empty listing
reducer -=1 # Lower the index by one at every iteration utilizing the reducer
print(reversed_list)
Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Reverse a Listing or an Array Utilizing Listing Comprehension
An inventory comprehension produces shorter code. And there’s no want for a brief variable as a result of listing comprehension acts on a listing in place.
To hold out the earlier operation, utilizing a listing comprehension:
reducer = len(languages)# Decrement the index inside a spread operate utilizing for loop in a listing comprehension
Reversed_list = [languages[reducer] for reducer in vary(reducer -1,-1,-1)]
print(Reversed_list)
Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Use the Slice Operator
The listing slice operator is fairly simple, though it has some limitations. For instance, you may not be capable to customise the output as you’d when utilizing a for loop.
Right here’s easy methods to reverse a listing utilizing the slice operator:
languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
rev_list = languages[::-1]
print(rev_list)Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
The [::-1] syntax is a intelligent shortcut that ends in a reversed listing. It truly means “copy each component of the listing, ranging from the top and counting backward” — i.e. “reverse it”!
Use an Array’s reverse Technique
That is one other methodology that acts in place: it modifies the unique array. This generally is a shortcoming since you may’t maintain the earlier listing for different operations.
Here is easy methods to reverse an array utilizing the reverse methodology:
languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
languages.reverse()
print(languages)Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Use the reversed Perform
The reversed operate iterates over a listing, array, or another sequence and returns its reversed copy. Nonetheless, you should explicitly declare the reversed output as a listing.
That is the way it works:
languages = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(listing(reversed(languages)))Output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Getting Inventive With Arrays
Arrays or lists are frequent methods to retailer information. Relying in your objective, you may need to current information to the consumer in reverse order. A technique to do that is to reverse the array or listing earlier than rendering it. As you’ve got seen, there are a few methods to invert a listing in Python. Select what works finest for you and aligns along with your logic for a particular downside.
You may also need to see easy methods to reverse an array in different programming languages to know the background logic higher.
Learn Subsequent
About The Writer