def Reverse(lst): return [element for element in reversed(lst)] lst = [3,14,8,24,1] print(Reverse(lst))
List Length in Python
The length of a list can be calculated in Python by using various methods. The built-in len() method in Python is widely used to calculate the length of any sequential data type. It calculates the number of elements or items in a list and returns the same as the length of the list. l=[2,4,6,8,10] print("The length of... Continue Reading →
Swapping the elements in a list
def swap(list, postn1, postn2): list[postn1], list[postn2] = list[postn2], list[postn1] return list List = [24,1,11,9,30,31] pos1, pos2 = 1, 3 print(swap(List, pos1-1, pos2-1))
Interchange first and last elements in a list in python
def swap_num_List(Listofnum): size = len(Listofnum) temp = Listofnum[0] Listofnum[0] = Listofnum[size - 1] Listofnum[size - 1] = temp return Listofnum Listofnum = [34,14,8,11,10] print(swap_num_List(Listofnum))