RPOPLPUSH
Removes the last element from a list and adds it to the beginning of another list.
Syntax
RPOPLPUSH source destination
Parameters
- source: The key of the list from which the element will be removed.
- destination: The key of the list to which the element will be added.
Details
- Available since: 1.0.0
- Time complexity: O(1)
- ACL categories:
@write,@list,@fast
The RPOPLPUSH command performs a combination of two operations:
- Removes the last element from the
sourcelist. - Adds the removed element to the beginning of the
destinationlist.
If source does not exist or is empty, the command returns nil and no changes are made. If destination does not exist, it is created as an empty list before the element is added.
Examples
Basic Usage
Assume we have the following lists:
RPUSH mylist1 "a" "b" "c"
RPUSH mylist2 "x" "y"
To move the last element from mylist1 to the beginning of mylist2:
RPOPLPUSH mylist1 mylist2
After running this command:
mylist1will contain:abmylist2will contain:cxy
The returned value will be:
"c"
Source List is Empty
If mylist1 was already empty:
RPOPLPUSH mylist1 mylist2
The command will return:
(nil)
And mylist2 will remain unchanged.
Destination List Does Not Exist
If mylist2 did not exist before:
RPOPLPUSH mylist1 mylist2
mylist2 will be created and will contain the element that was removed from mylist1.
Edge Cases
- If
sourcedoes not exist or is empty,RPOPLPUSHwill returnniland no changes will be made todestination. - If
destinationis the same assource, the element will be removed from the end of the list and added to the beginning of the same list.
RESP2/RESP3 Reply
- Bulk string reply: the element that was removed from the
sourcelist and added to thedestinationlist. If thesourcelist is empty or does not exist, the reply isnil.