Table of Contents
Introduction
In this section of python programming, we will understand the different ways to reverse the elements in the list.
Program
list = [1, 15, 13, 20, 14, 18, 9] list1 = [1, 15, 13, 20, 14, 18, 9] # Using reverse() def reverse_list(list): list.reverse() return list print("Using reverse()",reverse_list(list)) #Using reversed def reversed_list(list1): return [i for i in reversed(list1)] print("Using reversed()",reversed_list(list1))
Output
Explanation
In the above code, we have used reverse() and reversed() function to reverse the elements in the list. These two are python in-built function which easily reverse the list and provide us the output. In addition to this, we can also use slicing technique to reverse. But the drawback of using slicing is that copying the list requires more memory.
0 Comments