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

5 days 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 a COMMAND – LINDEX in redis-cli. 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.

The index is zero based, so 0 means the first element, 1 means the second element and so on. Negative number can also be used to designate the indexes of the list, here -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.

Example :-


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.