0
0
Redisquery~10 mins

List vs sorted set for sequences in Redis - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - List vs sorted set for sequences
Start
Choose data structure
List
Add elements at ends
Retrieve by index
Order is insertion order
Sorted Set
Add elements with scores
Retrieve by score or rank
Order by score
Unique elements only
You start by choosing either a List or a Sorted Set to store sequences. Lists keep order by insertion and allow duplicates. Sorted Sets store unique elements with scores that define order.
Execution Sample
Redis
LPUSH mylist A
LPUSH mylist B
ZADD myzset 1 A
ZADD myzset 2 B
ZRANGE myzset 0 -1 WITHSCORES
This shows adding elements to a Redis List and a Sorted Set, then retrieving them.
Execution Table
StepCommandActionData Structure StateOutput
1LPUSH mylist AAdd 'A' to head of listList: ['A']1 (new length)
2LPUSH mylist BAdd 'B' to head of listList: ['B', 'A']2 (new length)
3ZADD myzset 1 AAdd 'A' with score 1Sorted Set: {'A':1}1 (added)
4ZADD myzset 2 BAdd 'B' with score 2Sorted Set: {'A':1, 'B':2}1 (added)
5ZRANGE myzset 0 -1 WITHSCORESRetrieve all elements by score ascendingSorted Set unchanged['A', '1', 'B', '2']
6LRANGE mylist 0 -1Retrieve all elements by indexList unchanged['B', 'A']
7LPUSH mylist AAdd duplicate 'A' to headList: ['A', 'B', 'A']3 (new length)
8ZADD myzset 3 AUpdate score of 'A' to 3Sorted Set: {'A':3, 'B':2}0 (updated)
9ZRANGE myzset 0 -1 WITHSCORESRetrieve all elements by score ascendingSorted Set unchanged['B', '2', 'A', '3']
10LRANGE mylist 0 -1Retrieve all elements by indexList unchanged['A', 'B', 'A']
11EndNo more commandsFinal statesSee above
💡 Commands complete; final states show List allows duplicates and order by insertion; Sorted Set orders by score and keeps unique elements.
Variable Tracker
Data StructureStartAfter Step 1After Step 2After Step 3After Step 4After Step 7After Step 8Final
List 'mylist'[]['A']['B', 'A']['B', 'A']['B', 'A']['A', 'B', 'A']['A', 'B', 'A']['A', 'B', 'A']
Sorted Set 'myzset'{}{}{}{'A':1}{'A':1, 'B':2}{'A':1, 'B':2}{'A':3, 'B':2}{'A':3, 'B':2}
Key Moments - 2 Insights
Why does the List allow duplicate elements but the Sorted Set does not?
The execution_table rows 7 and 8 show that adding 'A' again to the List adds a new element (step 7), but adding 'A' to the Sorted Set updates its score instead of adding a duplicate (step 8). Sorted Sets enforce uniqueness by element.
How is the order of elements determined differently in List and Sorted Set?
Rows 2 and 4 show List order is by insertion at head (new elements appear first), while Sorted Set order is by score ascending (rows 5 and 9). This means List keeps insertion order, Sorted Set sorts by score.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7. What is the state of the List 'mylist'?
A['A', 'B', 'A']
B['B', 'A']
C['A', 'A', 'B']
D['B', 'A', 'B']
💡 Hint
Check the 'Data Structure State' column at step 7 in execution_table.
At which step does the Sorted Set update the score of an existing element?
AStep 4
BStep 8
CStep 3
DStep 7
💡 Hint
Look for 'Update score' action in the execution_table.
If you retrieve all elements from the Sorted Set after step 9, what order will they be in?
A['A', 'B']
B['A', 'A', 'B']
C['B', 'A']
D['B', 'A', 'B']
💡 Hint
Check the output of ZRANGE at step 9 in execution_table.
Concept Snapshot
Redis List stores sequences allowing duplicates and keeps insertion order.
Use LPUSH or RPUSH to add elements.
Retrieve by index with LRANGE.
Sorted Set stores unique elements with scores.
Use ZADD to add/update with score.
Retrieve ordered by score with ZRANGE.
Lists allow duplicates; Sorted Sets do not.
Lists order by insertion; Sorted Sets order by score.
Full Transcript
This visual execution compares Redis List and Sorted Set for sequences. Lists add elements at the head or tail and keep duplicates and insertion order. Sorted Sets add unique elements with scores that define order. The execution table shows commands adding elements, retrieving them, and updating scores. Lists allow duplicates as seen when 'A' is added twice. Sorted Sets update scores instead of duplicates. Retrieval from Lists returns elements in insertion order; Sorted Sets return elements ordered by score. This helps choose the right structure for your sequence needs.