LLEN
Returns the length of a list.
Syntax
LLEN keyDescription
The LLEN command returns the number of elements in a list. If the key does not exist, it returns 0. If the key exists but is not a list, an error is returned.
Arguments
key(required): The name of the list key.
Return Value
Returns an Integer: The length of the list, or 0 if the key doesn't exist.
Examples
Getting Length of Existing List
RPUSH mylist "a" "b" "c"
LLEN mylistResponse:
(integer) 3
(integer) 3Getting Length of Non-Existent List
LLEN nonexistentResponse:
(integer) 0Getting Length After Operations
RPUSH mylist "a" "b" "c"
LLEN mylist
LPOP mylist
LLEN mylistResponse:
(integer) 3
(integer) 3
"a"
(integer) 2Empty List
LPUSH mylist "item"
LPOP mylist
LLEN mylistResponse:
(integer) 1
"item"
(integer) 0Notes
- Returns
0if the key doesn't exist - Returns
0if the list is empty - Returns a
WRONGTYPEerror if the key exists but is not a list - The length is always a non-negative integer
Use Cases
- Queue Monitoring: Check how many items are in a queue
- Validation: Verify that a list has the expected number of elements
- Conditional Logic: Check if a list is empty before processing
- Statistics: Track the size of lists over time