Type: List¶
List is an orderly, repeatable, iterable type. It has LEFT and RIGHT directions and 8 manipulation, such as push, pop, range, set, delete, insert, specify index, trim.
Specifically
The range consists of start and stop (or end), it means 'start with' and 'stop with', unlike more programing language.
For example, in Python, list[3:6]
means list[start_with:stop]
, it will return list[3], list[4], list[5]
but NOT list[6]
.
In Redis, COMMAND list 3 6
means list[start_with:stop_with]
, it will return list[3], list[4], list[5]
, AND the list[6]
.
The index is same with Python.
element | a | b | c | d | e | f | g |
---|---|---|---|---|---|---|---|
positive | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
negative | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
LPUSH¶
LPUSH: means "push from left", in other words, add elements to list's head in order.
- Syntax:
LPUSH key element [element ...]
- Description: add elements to list's head in order. The key will be created if it isn't exists
RPUSH¶
RPUSH: in contrast, it means "push from right", in other words, add elements to list's tail in order.
- Syntax:
RPUSH key element [element ...]
- Description: the key will be created if it isn't exists.
LPUSHX¶
LPUSHX
- Syntax:
LPUSHX key element [element ...]
- Description: add elements to list's head in order if key is exists.
- Return: length of list, 0 if key isn't exists.
RPUSHX¶
RPUSHX
- Syntax:
RPUSHX key element [element ...]
- Description: add elements to list's tail in order if key is exists.
- Return: length of list, 0 if key isn't exists.
LPOP¶
LPOP: means "pop from left", in other words, get elements from list's head in order.
- Syntax:
LPOP key [count]
- Description: get count elements from list's head and delete them, count default 1.
- Return: count elements, nil if key isn't exists.
RPOP¶
RPOP: in contrast, it means "pop from right", in other words, get elements from list's tail in order.
- Syntax:
RPOP key [count]
- Description: get count elements from list's tail and delete them, count default 1.
- Return: count elements, nil if key isn't exists.
LRANGE¶
LRANGE
- Syntax:
LRANGE key start end
- Description: list the elements in the indicated range. -1 means until the end. More specifically, 'start' is 'start with', 'end' is 'end with'. The returns will include the 'end' elements.
- Return: elements in range, empty if key isn't exists or start index less than end index.
LLEN¶
LLEN
- Syntax:
LLEN key
- Description: return the length of key's list.
- Return: list's length, 0 if key isn't exists.
LSET¶
LSET
- Syntax:
LSET key index element
- Description: set the value to elements of key's list, index must be legal.
- Return: OK if successful, error if index is out of range or key isn't exists.
LINDEX¶
LINDEX
- Syntax:
LINDEX key index
- Description: get the element with index.
- Return: element, nil if index is out of range or key isn't exists.
LREM¶
LREM
- Syntax:
LREM key count element
- Description: remove the duplicate elements of indicated element.
- if count is negative, it will remove indicated element from list's tail.
- if count is positive, it will remove indicated element from list's head.
- if count is 0 or greater than actual count, it will remove all of indicated element.
- Return: count of removed elements. 0 if key isn't exists or elements isn't exists in list.
LTRIM¶
LTRIM
- Syntax:
LTRIM key start stop
- Description: LEAVE the elements in the range and REMOVE other elements.
- if start, except -1, is greater than stop, it will remove all elements.
- it equal the end if stop is out of range.
- Return: OK if successful.
LINSERT¶
LINSERT
- Syntax:
LINSERT key BEFORE|AFTER pivot element
- Description: insert a element before or after the pivot element.
- Return: the length of list after insert. 0 if fail.