Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to rotate a list in python

1 Answer

0 votes
from collections import deque

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 255] 

items = deque(lst)

items.rotate(1) # rotate right 1
lst_r = list(items)
print(lst_r)

items.rotate(-2) # rotate left 2
lst_r = list(items)
print(lst_r)


     
    
'''
run:
  
[255, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16]
[2, 3, 4, 5, 6, 7, 8, 9, 16, 255, 1]
    
'''

 





answered Mar 3, 2020 by avibootz

Related questions

1 answer 28 views
28 views asked Nov 16, 2022 by avibootz
1 answer 27 views
27 views asked Nov 16, 2022 by avibootz
...