RPOP
The RPOP
command in DiceDB is used to remove and return the last element of a list. This command is useful when processing elements in a Last-In-First-Out (LIFO) order.
Syntax
Parameters
Parameter | Description | Type | Required |
---|---|---|---|
key | The key of the list from which the last element will be removed. | String | Yes |
Return values
Condition | Return Value |
---|---|
The command is successful | String The value of the last element in the list |
The key does not exist | nil |
The key is of the wrong type | error |
Behaviour
- When the
RPOP
command is executed, DiceDB checks if the key exists and is associated with a list. - If the list has elements, the last element is removed and returned.
- If the key does not exist, the command treats it as an empty list and returns
nil
. - If the key exists but is not associated with a list, a
WRONGTYPE
error is returned. - If more than one key is passed, an error is returned.
Errors
-
Wrong type of value or key
:- Error Message:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- Occurs if the key exists but is not associated with a list.
- Error Message:
-
Wrong number of arguments
- Error Message:
(error) ERR wrong number of arguments for 'lpop' command
- Occurs if command is executed without any arguments or with 2 or more arguments
- Error Message:
Example Usage
Basic Usage
Non-Existent Key
Returns (nil)
if the provided key is non-existent
Invalid Usage: Key is Not a List
Trying to LPOP
a key mystring
which is holding wrong data type string
.
Invalid Usage: Wrong Number of Arguments
Passing more than one key will result in an error:
Best Practices
Check Key Type
: Before usingRPOP
, ensure that the key is associated with a list to avoid errors.Handle Non-Existent Keys
: Be prepared to handle the case where the key does not exist, asRPOP
will returnnil
in such scenarios.Use in Conjunction with Other List Commands
: TheRPOP
command is often used alongside other list commands likeRPUSH
,LPUSH
,LLEN
, andLPOP
to manage and process lists effectively.
By understanding the RPOP
command, you can effectively manage lists in DiceDB, ensuring that you can retrieve and process elements in a LIFO order.