Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rotatelistbyKtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'''We can use slice operator to rotate the list by any number of times clockwise(right rotation)
and anti-clockwise(left rotation) both'''

li=[1,2,3,4,5]
#k is number of times a list to be rotated
k=2
#for left rotation- anti-clockwise
li=li[k:]+li[:k]
print(li) #output: [3, 4, 5, 1, 2]

#for right rotation- clockwise
li=li[-k:]+li[:-k]
print(li) #output: [1, 2, 3, 4, 5]