Redis Lists

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.

⭐️ install javascript into your brain with the JavaScript Masterclass. -26 days ⭐️