Wednesday, July 21, 2021

Python for Beginners: How to reverse a list in Python

View Post

Lists in python are most used data structures. Often we need to  reverse a list, delete an element from the list, adding or inserting an element to the list or to merge two lists in python. In this article, we will look at different methods to reverse a list in python.

Reverse a list using loops

The simplest way to reverse a list is to iterate through the list in reverse order. While iterating, we can add each traversed element to a new list which will create a reversed list. For this, first we will calculate the length of the list and then we will iterate through the list in reverse order and we will append each traversed element to the new list using append() method as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
list_len=len(myList)
newList=[]
for i in range(list_len):
    newList.append(myList[list_len-1-i])
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Although we have created a reversed list by the above method, there is an overhead of calculating the length of the list before iterating. 

We can also reverse a list using a while loop. For this In each iteration, we will pop out an element from the original list using the pop() method until the list becomes empty as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=[]
while myList: #empty list points to None and will break the loop
    temp=myList.pop()
    newList.append(temp)

print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

This way of reversing a list  deletes all the elements from the original list. So you should use this way to reverse a list in python only when the original list is no longer needed in the program.

Reverse a list using slicing

Slicing is an operation with the help of which we can access, update or delete elements of the list. We can also use slicing to reverse the list as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=myList[::-1]
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Using reversed() method

We can use the reversed() method to reverse the list. The reversed() method takes an iterator like list as input and returns a reverse iterator. After that, we can create the reversed list from the iterator using a for loop as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
newList=[]
reverse_iterator=reversed(myList)
for i in reverse_iterator:
    newList.append(i)
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

We can also use list comprehension instead of the for loop to create a list from reverse iterator as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
reverse_iterator=reversed(myList)
newList= [i for i in reverse_iterator]
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

We can also convert the reversed iterator into a list directly using the list() constructor as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
reverse_iterator=reversed(myList)
newList=list(reverse_iterator)
print("reversed list is:",newList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Reverse list using reverse() method

In all of the methods described above, we have created a new list to store the value of the original list in reversed order. Now we will see how we can reverse a list in place i.e. how we can reverse the same list which will be provided to us as input. 

To reverse a list in this way, we can use the reverse() method. The reverse() method when invoked on a list, will reverse the order of the elements as follows.

myList=[1,2,3,4,5,6,7]
print("Original List is:",myList)
myList.reverse()
print("reversed list is:",myList)

Output:

Original List is: [1, 2, 3, 4, 5, 6, 7]
reversed list is: [7, 6, 5, 4, 3, 2, 1]

Conclusion

In this article we have seen how to reverse a list in python using a for loop, slicing, list comprehension, reverse() method and reversed() method. Stay tuned for more informative articles.

The post How to reverse a list in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...