LREM removes elements from a list that match a given value. You specify how many matching elements to remove and the value to remove.
Click to reveal answer
intermediate
How does the count parameter affect the LREM command?
The count parameter controls how many matching elements to remove: positive count removes from head to tail, negative count removes from tail to head, zero removes all matching elements.
Click to reveal answer
beginner
Syntax of the LREM command?
LREM key count value
Example: LREM mylist 2 "apple" removes up to 2 occurrences of "apple" from the list named mylist.
Click to reveal answer
beginner
What happens if count is zero in LREM?
If count is zero, LREM removes all elements equal to the given value from the list.
Click to reveal answer
beginner
Can LREM remove elements that do not exist in the list?
No, LREM only removes elements that match the value. If no elements match, the list remains unchanged and the command returns 0.
Click to reveal answer
What does LREM mylist 3 "orange" do?
ARemoves up to 3 occurrences of "orange" from the head of mylist
BRemoves all occurrences of "orange" from mylist
CRemoves 3 elements from the tail of mylist regardless of value
DAdds 3 "orange" elements to mylist
✗ Incorrect
LREM with positive count removes up to that many matching elements from head to tail.
What does LREM mylist -2 "banana" do?
ARemoves all "banana" elements from mylist
BRemoves 2 "banana" elements from the head of mylist
CRemoves 2 "banana" elements from the tail of mylist
DRemoves 2 elements from mylist regardless of value
✗ Incorrect
Negative count removes matching elements starting from the tail.
If count is zero in LREM, what happens?
AThe list is cleared
BOnly the first matching element is removed
CNo elements are removed
DAll matching elements are removed
✗ Incorrect
Count zero means remove all matching elements.
What does LREM return?
AThe removed elements themselves
BThe number of elements removed
CThe new length of the list
DAlways 0
✗ Incorrect
LREM returns how many elements it removed.
If the value does not exist in the list, what does LREM do?
ADoes nothing and returns 0
BRemoves the first element anyway
CRemoves all elements
DThrows an error
✗ Incorrect
If no matching elements exist, LREM does nothing and returns 0.
Explain how the count parameter affects the behavior of the LREM command in Redis.
Think about direction and how many elements are removed.
You got /3 concepts.
Describe a real-life example where you might use LREM to remove elements from a Redis list.
Imagine a list of things you want to update by removing some items.
You got /3 concepts.
Practice
(1/5)
1. What does the Redis command LREM mylist 2 "apple" do?
easy
A. Removes up to 2 occurrences of "apple" from the head of the list.
B. Removes all occurrences of "apple" from the list.
C. Removes 2 elements from the tail of the list regardless of value.
D. Removes the first 2 elements of the list.
Solution
Step 1: Understand the LREM command parameters
LREM takes a key, a count, and a value. The count 2 means remove up to 2 matching elements.
Step 2: Interpret the count sign and value
Positive count removes from head to tail. So it removes up to 2 "apple" elements starting from the front.
Final Answer:
Removes up to 2 occurrences of "apple" from the head of the list. -> Option A
Quick Check:
LREM with positive count removes from head [OK]
Hint: Positive count removes from head; negative from tail [OK]
Common Mistakes:
Thinking count removes elements regardless of value
Confusing positive and negative count directions
Assuming count removes all matches always
2. Which of the following is the correct syntax to remove exactly 3 occurrences of "banana" from the tail of a Redis list named fruits?
easy
A. LREM fruits 3 "banana"
B. LREM fruits -3 "banana"
C. LREM fruits 0 "banana"
D. LREM fruits "banana" 3
Solution
Step 1: Recall LREM syntax
The syntax is LREM key count value. Negative count removes from tail.
Step 2: Identify correct count for tail removal
To remove 3 occurrences from tail, count must be -3.
Final Answer:
LREM fruits -3 "banana" -> Option B
Quick Check:
Negative count removes from tail [OK]
Hint: Use negative count to remove from tail [OK]
Common Mistakes:
Using positive count to remove from tail
Swapping value and count positions
Using zero count to remove specific elements
3. Given a Redis list colors with elements ["red", "blue", "red", "green", "red"], what will be the list after executing LREM colors 2 "red"?
medium
A. ["blue", "red", "green", "red"]
B. ["red", "blue", "green"]
C. ["blue", "green", "red"]
D. ["red", "blue", "red", "green"]
Solution
Step 1: Identify elements and count
List is ["red", "blue", "red", "green", "red"]. LREM with count 2 removes up to 2 "red" from head.
Step 2: Remove first 2 "red" from head
First "red" at index 0 removed, second "red" at index 2 removed. Remaining elements: ["blue", "green", "red"]
Final Answer:
["blue", "green", "red"] -> Option C
Quick Check:
Removed first 2 "red" from head [OK]
Hint: Count 2 removes first 2 matches from head [OK]
Common Mistakes:
Removing all "red" elements instead of 2
Removing from tail instead of head
Confusing list order after removal
4. You run LREM mylist -1 "orange" but it does not remove any element. What could be the reason?
medium
A. The list mylist does not contain "orange".
B. Negative count cannot be used with LREM.
C. The command syntax is incorrect; value must come before count.
D. LREM only removes elements from the head, not the tail.
Solution
Step 1: Check command syntax and usage
LREM syntax is correct and negative count removes from tail.
Step 2: Consider why no removal happened
If no elements removed, likely the value "orange" is not in the list.
Final Answer:
The list mylist does not contain "orange". -> Option A
Quick Check:
No matching element means no removal [OK]
Hint: No matching value means LREM removes nothing [OK]
Common Mistakes:
Thinking negative count is invalid
Assuming syntax order is different
Believing LREM only removes from head
5. You have a Redis list tasks with elements ["email", "call", "email", "meeting", "email", "call"]. You want to remove all occurrences of "email" except the last one. Which sequence of commands achieves this?
hard
A. LREM tasks -2 "email"
B. LREM tasks 0 "email"
C. LREM tasks -5 "email"
D. LREM tasks 2 "email"
Solution
Step 1: Count total "email" elements
There are 3 "email" elements at positions 0, 2, 4.
Step 2: Removal strategy
Positive count removes matches from head-to-tail. Count 2 removes the first two "email"s, leaving the last one.
Step 3: Simulate execution
Remove index 0, list shifts to ["call", "email", "meeting", "email", "call"], then remove next match at new index 1 (orig 2). Result: ["call", "meeting", "email", "call"].
Final Answer:
LREM tasks 2 "email" -> Option D
Quick Check:
Positive count removes first matches from head [OK]
Hint: Positive count removes early matches from head [OK]
Common Mistakes:
Using negative count removes later matches from tail
Using zero count removes all occurrences
Using large negative count removes all if exceeds occurrences
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.