w3resource

Redis Lists: LINDEX

Redis LINDEX Command

Redis LINDEX command is used to get the element at index in the list stored at 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.

Here are some common uses and scenarios for the "LINDEX" command:

  • Random Access: Retrieve elements from a list at specific positions.
  • Data Inspection: Inspect the content of a list by accessing elements at various indices.
  • Validation: Verify the presence and value of elements at specific positions in a list.
  • Configuration Management: Access configuration values stored in list format by index.
  • Session Management: Retrieve session data or user attributes stored in lists based on their position.

Syntax:

redis 127.0.0.1:6379> LINDEX KEY_NAME INDEX_POSITION    

Available since

1.0.0.

Return Value

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

Return Value Type

String

Example: Redis LINDEX

127.0.0.1:6379> LPUSH mycolor1 white black red blue
(integer) 4
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "blue"
2) "red"
3) "black"
4) "white"
127.0.0.1:6379> LINDEX mycolor1 0
"blue"
127.0.0.1:6379> LINDEX mycolor1 1
"red"
127.0.0.1:6379> LINDEX mycolor1 -1
"white"
127.0.0.1:6379> LINDEX mycolor1 -2
"black"
127.0.0.1:6379> LINDEX mycolor1 6
(nil)

Previous: BRPOPLPUSH
Next: LINSERT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-lindex-key-index.php