📖 Scenario: You are managing a Redis database that stores a list of daily tasks for a small team. Each task is stored as a string in a Redis list called team_tasks. You want to learn how to access specific tasks by their position in the list.
🎯 Goal: Build a Redis command sequence to create a list of tasks, set a helper variable for the position, use LINDEX to get the task at that position, and finalize the setup.
📋 What You'll Learn
Create a Redis list called team_tasks with exactly these tasks in order: "Email clients", "Prepare report", "Team meeting", "Code review", "Deploy update"
Create a variable called task_position and set it to 2 (to access the third task, since Redis lists are zero-indexed)
Use the LINDEX command with team_tasks and task_position to get the task at that position
Add a final command to confirm the list length with LLEN for completeness
💡 Why This Matters
🌍 Real World
Managing task lists or queues in Redis is common in real-time applications like messaging, job scheduling, or caching.
💼 Career
Knowing how to access and manipulate Redis lists is valuable for backend developers, DevOps engineers, and anyone working with fast data storage.
Progress0 / 4 steps
1
Create the Redis list team_tasks with 5 tasks
Use the RPUSH command to create a Redis list called team_tasks with these exact tasks in order: "Email clients", "Prepare report", "Team meeting", "Code review", "Deploy update"
Redis
Hint
Use RPUSH to add multiple items to the list in one command.
2
Set a variable task_position to 2
Create a Redis variable called task_position and set it to 2 to represent the third task's index in the list.
Redis
Hint
Use the SET command to create a simple key-value variable.
3
Use LINDEX to get the task at task_position
Use the LINDEX command with the list team_tasks and the variable task_position to get the task at that position.
Redis
Hint
Remember Redis lists start at index 0, so index 2 is the third item.
4
Add a command to get the length of the team_tasks list
Add the LLEN command to get the length of the team_tasks list to confirm how many tasks are stored.
Redis
Hint
LLEN returns the number of items in a Redis list.
Practice
(1/5)
1. What does the Redis command LINDEX mylist 0 do?
easy
A. Deletes the first element of the list 'mylist'.
B. Returns the first element of the list 'mylist'.
C. Returns the length of the list 'mylist'.
D. Adds an element at the start of the list 'mylist'.
Solution
Step 1: Understand the LINDEX command
LINDEX returns the element at the specified index in a Redis list without modifying it.
Step 2: Interpret the index 0
Index 0 means the first element in the list.
Final Answer:
Returns the first element of the list 'mylist'. -> Option B
Quick Check:
LINDEX with 0 returns first element [OK]
Hint: LINDEX with 0 gets first item, -1 gets last [OK]
Common Mistakes:
Thinking LINDEX deletes or modifies the list
Confusing LINDEX with LLEN (length)
Using negative index incorrectly
2. Which of the following is the correct syntax to get the last element of a Redis list named tasks using LINDEX?
easy
A. LINDEX tasks -1
B. LINDEX tasks 1
C. LINDEX tasks 0
D. LINDEX tasks length
Solution
Step 1: Recall how to access last element
In Redis, negative index -1 means the last element in a list.
Step 2: Check the options
Only LINDEX tasks -1 uses -1 correctly to get the last element.
Final Answer:
LINDEX tasks -1 -> Option A
Quick Check:
Negative index -1 returns last element [OK]
Hint: Use -1 index to get last list item with LINDEX [OK]
Common Mistakes:
Using positive index 1 for last element
Using 'length' as index (invalid)
Confusing 0 with last element
3. Given a Redis list colors with elements ["red", "green", "blue", "yellow"], what is the result of LINDEX colors 2?
medium
A. "blue"
B. "yellow"
C. nil
D. "green"
Solution
Step 1: Identify list elements and indexes
List elements: 0:"red", 1:"green", 2:"blue", 3:"yellow".
Step 2: Find element at index 2
Index 2 corresponds to "blue".
Final Answer:
"blue" -> Option A
Quick Check:
Index 2 in list is "blue" [OK]
Hint: Count from 0 to find element at index [OK]
Common Mistakes:
Counting from 1 instead of 0
Confusing index 2 with last element
Expecting nil for valid index
4. You run LINDEX mylist 5 but get (nil) as a result. What is the most likely reason?
medium
A. The index 5 is negative and invalid.
B. LINDEX command is misspelled.
C. The list 'mylist' is empty but has 6 elements.
D. The list 'mylist' has fewer than 6 elements.
Solution
Step 1: Understand LINDEX behavior for out-of-range index
If the index is outside the list range, LINDEX returns nil.
Step 2: Analyze index 5
Index 5 means the 6th element (counting from 0). If list has fewer than 6 elements, result is nil.
Final Answer:
The list 'mylist' has fewer than 6 elements. -> Option D
Quick Check:
Out-of-range index returns nil [OK]
Hint: Index too big returns nil, check list length [OK]
Common Mistakes:
Assuming nil means syntax error
Thinking negative index 5 is invalid
Confusing empty list with list length
5. You have a Redis list fruits with elements ["apple", "banana", "cherry", "date"]. You want to get the second last element using LINDEX. Which command should you use?
hard
A. LINDEX fruits -1
B. LINDEX fruits 2
C. LINDEX fruits -2
D. LINDEX fruits 1
Solution
Step 1: Understand negative indexes in LINDEX
Negative indexes count from the end: -1 is last, -2 is second last.
Step 2: Identify second last element index
Second last element is at index -2.
Final Answer:
LINDEX fruits -2 -> Option C
Quick Check:
Use -2 to get second last element [OK]
Hint: Use -2 index for second last element [OK]
Common Mistakes:
Using positive index 2 (third element)
Using -1 which is last element
Confusing index 1 with second last
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.