Redis LINDEX – How to get element from specific index of a list in redis

2 months ago Lalit Bhagtani 0

In this tutorial, we will learn about how to get an element from a specific index of the list value. For this, we will use Redis LINDEX command.

LINDEX Command

This command returns an element stored at the specified index of the list value stored at a key. The index is zero based, so 0 means the first element, 1 means the second element and so on. A negative number can also be used to designate the indexes of the list, where -1 means the last element, -2 means second last element and so on. An error is returned when LINDEX command is called with an index value which is out of the range. The syntax of redis LINDEX command is as follows :-

Syntax :-

redis host:post> LINDEX <key name> <index>

Output :- 

- (string) value, representing an element of the list at index <index>.
- (error) ERR index out of range, if <index> value is out of range.
- Error, if key exist and value stored at the key is not a list.

Example :-

Redis LINDEX

References :-

  1. LINDEX Command Docs

That’s all for how to get an element from a specific index of the list value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- List Commands