Skip to content

Redis Lists

New Course Coming Soon:

Get Really Good at Git

A list is a set of key-values pairs linked to each other.

LPUSH and RPUSH are the two commands to work with lists.

You use the command LPUSH <listkey> <value> to create the first item.

Example:

LPUSH names "Flavio"

Then subsequent items can be added at the bottom of the list: RPUSH <listkey> <value>

Or at the top of the list with LPUSH <listkey> <value>.

Example:

LPUSH names "Flavio"
LPUSH names "Syd"
RPUSH names "Roger"

You can add duplicate values into a list.

LPUSH names "Flavio"
LPUSH names "Flavio"
RPUSH names "Flavio"

A list can hold a big number of items, more than 4 billions.

Count how many items are in a list with LLEN <listkey>.

Get and remove the last item in a list with RPOP <listkey>. Do the same with the first item with LPOP.

Remove multiple items from the list using the LREM command.

You can limit how long a list is using LTRIM.

LTRIM names 0 1 cuts the list to just 2 items, item at position 0 (the first) and item at position 1.

Using LRANGE you can get the items in the list.

LRANGE names 0 100 returns items starting at position 0 (the beginning), ending at position 100.

LRANGE names 0 0 returns the item in position 0 (the first).

LRANGE names 2 2 returns the item in position 2.

LRANGE names 0 -1 lists all items.

See all the lists commands here.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!

Here is how can I help you: