Redis Lindex Command
The Redis Lindex command is used to get an element from a list by its index. You can also use negative indices, where -1 is the last element, -2 is the second-to-last element, and so on.
Syntax
The basic syntax of the redis Lindex command is as follows:
redis 127.0.0.1:6379> LINDEX KEY_NAME INDEX_POSITION
Available Version
= 1.0.0
Return Value
The element at the specified index in the list. Returns nil if the specified index is out of range.
Example
redis 127.0.0.1:6379> LPUSH mylist "World"
(integer) 1
redis 127.0.0.1:6379> LPUSH mylist "Hello"
(integer) 2
redis 127.0.0.1:6379> LINDEX mylist 0
"Hello"
redis 127.0.0.1:6379> LINDEX mylist -1
"World"
redis 127.0.0.1:6379> LINDEX mylist 3 # index不在 mylist 的区间范围内
(nil)
Redis List