0
0
Redisquery~5 mins

LINDEX for position access in Redis

Choose your learning style9 modes available
Introduction
LINDEX helps you get an item from a list by its position. It is like picking a book from a shelf by its place number.
You want to see the first or last item in a list.
You need to check a specific item in a list without removing it.
You want to quickly find an element at a known position.
You are debugging and want to peek at list contents.
You want to display a single list item in a user interface.
Syntax
Redis
LINDEX key index
Index starts at 0 for the first item.
Negative index counts from the end, -1 is the last item.
Examples
Gets the first item from the list named 'mylist'.
Redis
LINDEX mylist 0
Gets the last item from the list named 'mylist'.
Redis
LINDEX mylist -1
Gets the third item (index 2) from 'mylist'.
Redis
LINDEX mylist 2
Sample Program
First, we add three fruits to 'mylist'. Then we get the item at position 1 (second item).
Redis
RPUSH mylist apple banana cherry
LINDEX mylist 1
OutputSuccess
Important Notes
If the index is out of range, LINDEX returns nil (no value).
LINDEX does not remove the item; it only reads it.
Lists in Redis keep order, so index 0 is always the first pushed item.
Summary
LINDEX gets an item from a list by its position number.
Use 0 for the first item, -1 for the last item.
It is a safe way to peek at list contents without changing them.