Redis - List Lindex Command



Redis LINDEX command is used to get the element at the index in the list stored at the key. The index is zero-based, so 0 means the first element, 1 the second element, and so on. Negative indices can be used to designate elements starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate, and so forth.

Return Value

String reply, the requested element, or nil when the index is out of range.

Syntax

Following is the basic syntax of Redis LINDEX command.

redis 127.0.0.1:6379> LINDEX KEY_NAME INDEX_POSITION

Example

redis 127.0.0.1:6379> LPUSH list1 "foo" 
(integer) 1 
redis 127.0.0.1:6379> LPUSH list1 "bar" 
(integer) 2 
redis 127.0.0.1:6379> LINDEX list1 0 
"foo" 
redis 127.0.0.1:6379> LINDEX list1 -1 
"bar" 
redis 127.0.0.1:6379> LINDEX list1 5 
nil 
redis_lists.htm
Advertisements