Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

rotate list python

# rotate right with slicing

def rotate_list_right(lst, rotation_value):

    result_list =lst[-rotation_value:]+lst[:-rotation_value]
    print(result_list)

rotate_list_right([1,2,3,4,5,6],1)
rotate_list_right([1,2,3,4,5,6],2)

# rotate right with for loop

def rotate_list_right(lst, rotation_value):

    result_list =lst.copy()
    
    for k in range(0, len(lst)):
        result_list[k] = lst[k-rotation_value]
    print(result_list)
    
rotate_list_right([1,2,3,4,5,6],1)
rotate_list_right([1,2,3,4,5,6],2)
 
PREVIOUS NEXT
Tagged: #rotate #list #python
ADD COMMENT
Topic
Name
9+4 =