LPUSH and RPUSH add items to a list in Redis. They help you store and manage ordered data easily.
LPUSH and RPUSH for insertion in Redis
LPUSH key value [value ...] RPUSH key value [value ...]
LPUSH adds values to the start (left) of the list.
RPUSH adds values to the end (right) of the list.
LPUSH mylist "apple"RPUSH mylist "banana"LPUSH mylist "cherry" "date"
RPUSH mylist "elderberry" "fig"
This example clears the list 'fruits', then adds items using LPUSH and RPUSH. Finally, it shows the full list.
DEL fruits LPUSH fruits "apple" RPUSH fruits "banana" LPUSH fruits "cherry" "date" RPUSH fruits "elderberry" "fig" LRANGE fruits 0 -1
LPUSH inserts items in the order they are given, so the last value ends up at the front.
RPUSH inserts items in the order they are given, so the first value ends up just after the existing last element.
If the list does not exist, Redis creates it automatically when you use LPUSH or RPUSH.
LPUSH adds items to the start (left) of a Redis list.
RPUSH adds items to the end (right) of a Redis list.
Both commands create the list if it does not exist.
Practice
LPUSH mylist "apple" do?Solution
Step 1: Understand LPUSH command behavior
The LPUSH command inserts the given element(s) at the start (left) of the list.Step 2: Apply to the example
LPUSH mylist "apple" adds "apple" to the left side of the list named mylist.Final Answer:
Adds "apple" to the start (left) of the list named mylist. -> Option AQuick Check:
LPUSH = insert left [OK]
- Confusing LPUSH with RPUSH and thinking it adds to the end.
- Assuming LPUSH removes elements instead of adding.
- Believing LPUSH creates a list without adding elements.
fruits?Solution
Step 1: Identify command to add at the end
RPUSH adds elements to the end (right) of a Redis list.Step 2: Check syntax correctness
RPUSH fruits "banana" correctly adds "banana" to the end of the list named fruits.Final Answer:
RPUSH fruits "banana" -> Option AQuick Check:
RPUSH = insert right [OK]
- Using LPUSH instead of RPUSH to add at the end.
- Using non-existent commands like PUSH or APPEND.
- Incorrect command order or missing quotes.
colors, what will be the result of these commands executed in order?LPUSH colors "red"RPUSH colors "blue"LPUSH colors "green"What is the content of the list
colors after these commands?Solution
Step 1: Apply LPUSH colors "red" on empty list
List becomes ["red"] because LPUSH adds "red" to the start.Step 2: Apply RPUSH colors "blue"
List becomes ["red", "blue"] because RPUSH adds "blue" to the end.Step 3: Apply LPUSH colors "green"
List becomes ["green", "red", "blue"] because LPUSH adds "green" to the start.Final Answer:
["green", "red", "blue"] -> Option DQuick Check:
LPUSH adds left, RPUSH adds right [OK]
- Mixing order of elements after LPUSH and RPUSH.
- Assuming RPUSH adds to the start.
- Ignoring the order of commands executed.
LPUSH mylist "orange" "lemon" but get a "WRONGTYPE Operation against a key holding the wrong kind of value" error. What is the likely cause?Solution
Step 1: Check LPUSH command syntax
LPUSH supports multiple values in one command and creates a list if the key does not exist.Step 2: Identify the WRONGTYPE error cause
The error occurs because the key 'mylist' already exists but holds a different data type (not a list).Final Answer:
The key mylist exists but is not a list. -> Option BQuick Check:
LPUSH + WRONGTYPE = key exists wrong type [OK]
- Assuming LPUSH cannot add multiple values at once.
- Thinking list must exist before LPUSH.
- Misordering command arguments.
tasks with the elements ["task1", "task2", "task3"] in that exact order, where task1 is the first element and task3 is the last. Which sequence of commands will achieve this?Solution
Step 1: Understand desired order
We want the list as ["task1", "task2", "task3"] with task1 first and task3 last.Step 2: Choose commands to add elements in order
RPUSH adds elements to the end, so adding task1, then task2, then task3 with RPUSH keeps the order correct.Step 3: Analyze other options
LPUSH adds to start, so LPUSH tasks "task1"; LPUSH tasks "task2"; LPUSH tasks "task3" reverses order; LPUSH tasks "task1"; RPUSH tasks "task2"; LPUSH tasks "task3" mixes commands causing wrong order; RPUSH tasks "task3"; RPUSH tasks "task2"; RPUSH tasks "task1" reverses order.Final Answer:
RPUSH tasks "task1"; RPUSH tasks "task2"; RPUSH tasks "task3" -> Option CQuick Check:
RPUSH in order = correct list order [OK]
- Using LPUSH in order, which reverses the list.
- Mixing LPUSH and RPUSH causing unexpected order.
- Adding elements in reverse order.
Ready to prove it?
Pass the test and this pattern moves unknown
