Skip to content

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.


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about redis: