Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I move the last item in a list to the front in python?

Tags:

I searched thoroughly but can't find anything relating to this exact specific. I have a list:

a = [two, three, one] 

I want to move one to the front, so it becomes:

a = [one, two, three] 

The thing is, it could be ANY amount of numbers in the list. Assume there is no way of knowing whether there will be 50 items or 3.

like image 236
Jared Avatar asked Jun 27 '11 08:06

Jared


1 Answers

Basically:

a.insert(0, a.pop()) 

Consider using collections.deque if you're doing that often, though.

like image 160
Cat Plus Plus Avatar answered Sep 22 '22 08:09

Cat Plus Plus