Search code examples
pythonlistreverse

Python : How to reverse the order of sublist in a list?


I have a list which is made up of nested lists. I want to rearrange the order of sublists. I tried using the list.reverse() method, however it reverses the elements of the sublist. I want to reverse just the order of sublist rather than the elements.

Input : [['25', '9', 'BID', 'toaster_1', '17.00'], ['26', '11', 'BID', 'toaster_1', '17.00']]
Expected Output : [['26', '11', 'BID', 'toaster_1', '17.00'],['25', '9', 'BID', 'toaster_1', '17.00']]

Solution

  • initial_list=[['25', '9', 'BID', 'toaster_1', '17.00'], ['26', '11', 'BID', 'toaster_1', '17.00']]
    modified_list=initial_list[::-1] #reverse the list
    print(modified_list)
    

    The 2nd line in the code reverse the list.From index value -1