Redis Command: LMPOP

Summary

Command NameLMPOP
UsagePop items from one of multiple list
Group list
ACL Category@write
@list
@slow
Time ComplexityO(N+M)
FlagWRITE
Arity-4

Notes

  • In the time complexity, N is the number of list name keys and M is the number of returned elements.

Signature

LMPOP <number_of_keys> <key> [<key>] ( LEFT | RIGHT ) [ COUNT <count> ]

Usage

LMPOP command pops one or more items from list(s). This operation can be performed on multiple lists. In case of multiple lists, the pop operation will be performed on the first non-empty list.

Notes

  • This command is useful when we have multiple lists serving similar purposes, and we want to pop items from any of the lists if there are items in it.

Arguments

ParameterGroupDescriptionNameTypeMultipleOptional
<number_of_keys>Number of keys to popnumkeysinteger
<key>key name of the listkeykeyTrue
LEFTwherePop from the left/HEAD of the listleftpure-token
RIGTHwherePop from the right/TAIL of the listrightpure-token
<count>Number of items to returncountintegerTrue

Notes

  • <count> should be greater than Zero(0). If count <count> is less than or equal to zero(0), then following error is returned-
    (error) ERR count should be greater than 0

Return Value

Return valueCase for the return valueType
Item nameIf one item was popped from liststring
List of itemIf multiple items popped from listarray[string]
(nil)If no item was poppednull
errorIf the key exists but the data type of the key is not a listerror

Notes

  • This command returns the name of the list form which list the data being returned and then the popped items from the list.
  • Number of returned elements is the lowest number between the <count> argument and the size of the first non-empty list.
  • If the command is applied on ken which is not of type list, then the following error is returned-
    (error) WRONGTYPE Operation against a key holding the wrong kind of value

Command Examples

Here are a few examples of the LMPOP command usage-

# Redis LMPOP command examples

# Create list "bigboxlist" and push items
127.0.0.1:6379> rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
(integer) 5

# Check list items
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "big list item 1"
2) "big list item 2"
3) "big list item 3"
4) "big lits item 4"
5) "big list item 5"

# Create and push items in "smallboxlist"
127.0.0.1:6379> rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
(integer) 3

# check item from list
127.0.0.1:6379> lrange smallboxlist 0 -1
1) "small list item 1"
2) "small list item 2"
3) "small list item 3"

# Use LMPOP on bigboxlist and pop item form left
127.0.0.1:6379> lmpop 1 bigboxlist LEFT
1) "bigboxlist"
2) 1) "big list item 1"

# Pop 2 items from the LEFT of bigboxlist
127.0.0.1:6379> lmpop 1 bigboxlist LEFT count 2
1) "bigboxlist"
2) 1) "big list item 2"
   2) "big list item 3"

# Try to pop items from any of bigboxlist or smallboxlist
# Items popped from bigboxlist as this list still has item
127.0.0.1:6379> lmpop 2 bigboxlist smallboxlist LEFT count 5
1) "bigboxlist"
2) 1) "big lits item 4"
   2) "big list item 5"

# Try to pop again from any of bigbostlist or smallboxlist
# Items poped from smallboxlist, as there is no item in bigboxlist
127.0.0.1:6379> lmpop 2 bigboxlist smallboxlist LEFT count 5
1) "smallboxlist"
2) 1) "small list item 1"
   2) "small list item 2"
   3) "small list item 3"

# Try to pop from a non existing list
# It returns (nil)
127.0.0.1:6379> lmpop 1 nonexistinglist LEFT count 5
(nil)

# Push some items in bigboxlist for continuing the test
127.0.0.1:6379> rpush bigboxlist "item a" "item b" "item c" "item d"
(integer) 4

# Try to pop item from any of a non existing list or bigboxlist
# items popped from bigboxlist and the non existing list is ignored
127.0.0.1:6379> lmpop 2 nonexistinglist bigboxlist LEFT count 5
1) "bigboxlist"
2) 1) "item a"
   2) "item b"
   3) "item c"
   4) "item d"

# Set a string value
127.0.0.1:6379> set bigboxstr "My big box string"
OK

# Try to pop from a string item
# It returns an error
127.0.0.1:6379> lmpop 1 bigboxstr right
(error) WRONGTYPE Operation against a key holding the wrong kind of value

# Try to pop items from a string and a list
# we get an error as the string is the first item and the command tries to pop items from the string
127.0.0.1:6379> lmpop 2 bigboxstr bigboxlist right
(error) WRONGTYPE Operation against a key holding the wrong kind of value

# Try to pop items from a list and string
# we get data if the list is non empty
127.0.0.1:6379> lmpop 2 bigboxlist bigboxstr right
1) "bigboxlist"
2) 1) "big list item 5"

# Count should be greater than zero(0)
127.0.0.1:6379> lmpop 1 bigboxlist left count 0
(error) ERR count should be greater than 0

Notes

  • If the command already gets items from some non-empty list, and there are some invalid items(not a list) later, then it will not throw any error.

Code Implementations

Here are the usage examples of the Redis LMPOP command in different programming languages.

// Redis LMPOP command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	// Create list "bigboxlist" and push items
	// Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
	// Result: (integer) 5
	rpushResult, err := rdb.RPush(ctx, "bigboxlist", "big list item 1", "big list item 2", "big list item 3", "big lits item 4", "big list item 5").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Error: " + err.Error())
	}

	fmt.Println("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: ", rpushResult)

	// Check list items
	// Command: lrange bigboxlist 0 -1
	// Result:
	//      1) "big list item 1"
	//      2) "big list item 2"
	//      3) "big list item 3"
	//      4) "big lits item 4"
	//      5) "big list item 5"
	lrangeResult, err := rdb.LRange(ctx, "bigboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange bigboxlist 0 -1 | Result: ", lrangeResult)

	// Create and push items in "smallboxlist"
	// Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
	// Result: (integer) 3
	rpushResult, err = rdb.RPush(ctx, "smallboxlist", "small list item 1", "small list item 2", "small list item 3").Result()

	if err != nil {
		fmt.Println("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Error: " + err.Error())
	}

	fmt.Println("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: ", rpushResult)

	// check item from list
	// Command: lrange smallboxlist 0 -1
	// Result:
	//     1) "small list item 1"
	//     2) "small list item 2"
	//     3) "small list item 3"
	lrangeResult, err = rdb.LRange(ctx, "smallboxlist", 0, -1).Result()

	if err != nil {
		fmt.Println("Command: lrange smallboxlist 0 -1 | Error: " + err.Error())
	}

	fmt.Println("Command: lrange smallboxlist 0 -1 | Result: ", lrangeResult)

	// Use LMPOP on bigboxlist and pop item form left
	// Command: lmpop 1 bigboxlist LEFT
	// Result:
	//     1) "bigboxlist"
	//     2) 1) "big list item 1"
	lmpopList, lmpopResult, err := rdb.LMPop(ctx, "LEFT", 1, "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 1 bigboxlist LEFT | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 1 bigboxlist LEFT | Result: ", lmpopList, lmpopResult)

	// Pop 2 items from the LEFT of bigboxlist
	// Command: lmpop 1 bigboxlist LEFT count 2
	// Result:
	//     1) "bigboxlist"
	//     2)      1) "big list item 2"
	//             2) "big list item 3"
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "LEFT", 2, "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 1 bigboxlist LEFT count 2 | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 1 bigboxlist LEFT count 2 | Result: ", lmpopList, lmpopResult)

	// Try to pop items from any of bigboxlist or smallboxlist
	// Items popped from bigboxlist as this list still has item
	// Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
	// Result:
	//     1) "bigboxlist"
	//     2)      1) "big lits item 4"
	//             2) "big list item 5"
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "LEFT", 2, "bigboxlist", "smallboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ", lmpopList, lmpopResult)

	// Try to pop again from any of bigbostlist or smallboxlist
	// Items poped from smallboxlist, as there is no item in bigboxlist
	// Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
	// Result:
	//     1) "smallboxlist"
	//     2)      1) "small list item 1"
	//             2) "small list item 2"
	//             3) "small list item 3"
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "LEFT", 5, "bigboxlist", "smallboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ", lmpopList, lmpopResult)

	// Try to pop from a non existing list
	// It returns (nil)
	// Command: lmpop 1 nonexistinglist LEFT count 5
	// Result: (nil)
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "LEFT", 5, "nonexistinglist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 1 nonexistinglist LEFT count 5 | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 1 nonexistinglist LEFT count 5 | Result: ", lmpopList, lmpopResult)

	// Push some items in bigboxlist for continuing the test
	// Command: rpush bigboxlist "item a" "item b" "item c" "item d"
	// Result: (integer) 4
	rpushResult, err = rdb.RPush(ctx, "bigboxlist", "item a", "item b", "item c", "item d", "item e", "item f", "item g", "item h").Result()

	if err != nil {
		fmt.Println("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Error: " + err.Error())
	}

	fmt.Println("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: ", rpushResult)

	// Try to pop item from any of a non existing list or bigboxlist
	// items popped from bigboxlist and the non existing list is ignored
	// Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
	// Result:
	//         1) "bigboxlist"
	//         2)      1) "item a"
	//                 2) "item b"
	//                 3) "item c"
	//                 4) "item d"
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "LEFT", 5, "nonexistinglist", "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: ", lmpopList, lmpopResult)

	// Set a string value
	// Command: set bigboxstr "My big box string"
	// Result: OK
	setResult, err := rdb.Set(ctx, "bigboxstr", "My big box string", 0).Result()

	if err != nil {
		fmt.Println("Command: set bigboxstr "My big box string" | Error: " + err.Error())
	}

	fmt.Println("Command: set bigboxstr "My big box string" | Result: " + setResult)

	// Try to pop from a string item
	// It returns an error
	// Command: lmpop 1 bigboxstr right
	// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "RIGHT", 1, "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: lmpop 1 bigboxstr right | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 1 bigboxstr right | Result: ", lmpopList, lmpopResult)

	// Try to pop items from a string and a list
	// we get an error as the string is the first item and the command tries to pop items from the string
	// Command: lmpop 2 bigboxstr bigboxlist right
	// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "RIGHT", 1, "bigboxstr", "bigboxlist").Result()

	if err != nil {
		fmt.Println("Command: lmpop 2 bigboxstr bigboxlist right | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 2 bigboxstr bigboxlist right | Result: ", lmpopList, lmpopResult)

	// Try to pop items from a list and string
	// we get data if the list is non empty
	// Command: lmpop 2 bigboxlist bigboxstr right
	// Result:
	//      1) "bigboxlist"
	//      2)      1) "big list item 5"
	lmpopList, lmpopResult, err = rdb.LMPop(ctx, "RIGHT", 1, "bigboxlist", "bigboxstr").Result()

	if err != nil {
		fmt.Println("Command: lmpop 2 bigboxlist bigboxstr right | Error: " + err.Error())
	}

	fmt.Println("Command: lmpop 2 bigboxlist bigboxstr right | Result: ", lmpopList, lmpopResult)

}

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result:  5
Command: lrange bigboxlist 0 -1 | Result:  [big list item 1 big list item 2 big list item 3 big lits item 4 big list item 5]
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result:  3
Command: lrange smallboxlist 0 -1 | Result:  [small list item 1 small list item 2 small list item 3]

Command: lmpop 1 bigboxlist LEFT | Result:  bigboxlist [big list item 1]
Command: lmpop 1 bigboxlist LEFT count 2 | Result:  bigboxlist [big list item 2 big list item 3]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result:  bigboxlist [big lits item 4 big list item 5]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result:  smallboxlist [small list item 1 small list item 2 small list item 3]
Command: lmpop 1 nonexistinglist LEFT count 5 | Error: redis: nil
Command: lmpop 1 nonexistinglist LEFT count 5 | Result:   []
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result:  8
Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result:  bigboxlist [item a item b item c item d item e]
Command: set bigboxstr "My big box string" | Result: OK
Command: lmpop 1 bigboxstr right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 1 bigboxstr right | Result:   []

Command: lmpop 2 bigboxstr bigboxlist right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxstr bigboxlist right | Result:   []
Command: lmpop 2 bigboxlist bigboxstr right | Result:  bigboxlist [item h]

Notes

  • Use “LMPop” method from redis-go module.
  • Signature of the methods is-
    func (c cmdable) LMPop(ctx context.Context, direction string, count int64, keys …string) *KeyValuesCmd
// Redis LMPOP command example in JavaScript(NodeJS)

import { createClient } from "redis";

// Create redis client
const redisClient = createClient({
  url: "redis://default:@localhost:6379",
});

redisClient.on("error", (err) =>
  console.log("Error while connecting to Redis", err)
);

// Connect Redis client
await redisClient.connect();

/**
 * Create list "bigboxlist" and push items
 *
 * Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
 * Result: (integer) 5
 */
let commandResult = await redisClient.rPush("bigboxlist", [
  "big list item 1",
  "big list item 2",
  "big list item 3",
  "big lits item 4",
  "big list item 5",
]);

console.log(
  'Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: ' +
    commandResult
);

/**
 * Check list items
 *
 * Command: lrange bigboxlist 0 -1
 * Result:
 *      1) "big list item 1"
 *      2) "big list item 2"
 *      3) "big list item 3"
 *      4) "big lits item 4"
 *      5) "big list item 5"
 */
commandResult = await redisClient.lRange("bigboxlist", 0, -1);

console.log("Command: lrange bigboxlist 0 -1 | Result: " + commandResult);

/**
 * Create and push items in "smallboxlist"
 *
 * Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
 * Result: (integer) 3
 */
commandResult = await redisClient.rPush(
  "smallboxlist",
  "small list item 1",
  "small list item 2",
  "small list item 3"
);

console.log(
  'Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: ' +
    commandResult
);

/**
 * check item from list
 *
 * Command: lrange smallboxlist 0 -1
 * Result:
 *     1) "small list item 1"
 *     2) "small list item 2"
 *     3) "small list item 3"
 */
commandResult = await redisClient.lRange("smallboxlist", 0, -1);

console.log("Command: lrange smallboxlist 0 -1 | Result: " + commandResult);

/**
 * Use LMPOP on bigboxlist and pop item form left
 *
 * Command: lmpop 1 bigboxlist LEFT
 * Result:
 *     1) "bigboxlist"
 *     2) 1) "big list item 1"
 */
commandResult = await redisClient.lmPop("bigboxlist", "LEFT");

console.log("Command: lmpop 1 bigboxlist LEFT | Result: " + commandResult);

/**
 * Pop 2 items from the LEFT of bigboxlist
 *
 * Command: lmpop 1 bigboxlist LEFT count 2
 * Result:
 *     1) "bigboxlist"
 *     2)      1) "big list item 2"
 *             2) "big list item 3"
 */
commandResult = await redisClient.lmPop("bigboxlist", "LEFT", { COUNT: 2 });

console.log(
  "Command: lmpop 1 bigboxlist LEFT count 2 | Result: " + commandResult
);

/**
 * Try to pop items from any of bigboxlist or smallboxlist
 * Items popped from bigboxlist as this list still has item
 *
 * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
 * Result:
 *     1) "bigboxlist"
 *     2)      1) "big lits item 4"
 *             2) "big list item 5"
 */
commandResult = await redisClient.lmPop(
  ["bigboxlist", "smallboxlist"],
  "LEFT",
  { COUNT: 2 }
);

console.log(
  "Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: " +
    commandResult
);

/**
 * Try to pop again from any of bigbostlist or smallboxlist
 * Items poped from smallboxlist, as there is no item in bigboxlist
 *
 * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
 * Result:
 *     1) "smallboxlist"
 *     2)      1) "small list item 1"
 *             2) "small list item 2"
 *             3) "small list item 3"
 */
commandResult = await redisClient.lmPop(
  ["bigboxlist", "smallboxlist"],
  "LEFT",
  { COUNT: 5 }
);

console.log(
  "Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: " +
    commandResult
);

/**
 * Try to pop from a non existing list
 * It returns (nil)
 *
 * Command: lmpop 1 nonexistinglist LEFT count 5
 * Result: (nil)
 */
commandResult = await redisClient.lmPop("nonexistinglist", "LEFT", {
  COUNT: 5,
});

console.log(
  "Command: lmpop 1 nonexistinglist LEFT count 5 | Result: " + commandResult
);

/**
 * Push some items in bigboxlist for continuing the test
 * Command: rpush bigboxlist "item a" "item b" "item c" "item d"
 * Result: (integer) 4
 */
commandResult = await redisClient.rPush(
  "bigboxlist",
  "item a",
  "item b",
  "item c",
  "item d",
  "item e",
  "item f",
  "item g",
  "item h"
);

console.log(
  'Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: ' +
    commandResult
);

/**
 * Try to pop item from any of a non existing list or bigboxlist
 * items popped from bigboxlist and the non existing list is ignored
 * Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
 * Result:
 *         1) "bigboxlist"
 *         2)      1) "item a"
 *                 2) "item b"
 *                 3) "item c"
 *                 4) "item d"
 */
commandResult = await redisClient.lmPop(
  ["nonexistinglist", "bigboxlist"],
  "LEFT",
  { COUNT: 5 }
);

console.log(
  "Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: " +
    commandResult
);

/**
 * Set a string value
 *
 * Command: set bigboxstr "My big box string"
 * Result: OK
 */
commandResult = await redisClient.set("bigboxstr", "My big box string");

console.log(
  'Command: set bigboxstr "My big box string" | Result: ' + commandResult
);

/**
 * Try to pop from a string item
 * It returns an error
 * Command: lmpop 1 bigboxstr right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.lmPop("bigboxstr", "LEFT");

  console.log("Command: lmpop 1 bigboxstr right | Result: " + commandResult);
} catch (err) {
  console.log("Command: lmpop 1 bigboxstr right | Error: ", err);
}

/**
 * Try to pop items from a string and a list
 * we get an error as the string is the first item and the command tries to pop items from the string
 *
 * Command: lmpop 2 bigboxstr bigboxlist right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
  commandResult = await redisClient.lmPop(["bigboxstr", "bigboxlist"], "RIGHT");

  console.log(
    "Command: lmpop 2 bigboxstr bigboxlist right | Result: " + commandResult
  );
} catch (err) {
  console.log("Command: lmpop 2 bigboxstr bigboxlist right | Error: ", err);
}

/**
 * Try to pop items from a list and string
 * we get data if the list is non empty
 *
 * Command: lmpop 2 bigboxlist bigboxstr right
 * Result:
 *      1) "bigboxlist"
 *      2)      1) "big list item 5"
 */
try {
  commandResult = await redisClient.lmPop(["bigboxlist", "bigboxstr"], "RIGHT");

  console.log(
    "Command: lmpop 2 bigboxlist bigboxstr right | Result: " + commandResult
  );
} catch (err) {
  console.log("Command: lmpop 2 bigboxlist bigboxstr right | Error: ", err);
}

process.exit(0);

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: big list item 1,big list item 2,big list item 3,big lits item 4,big list item 5
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 1
Command: lrange smallboxlist 0 -1 | Result: small list item 1

Command: lMpop 1 bigboxlist LEFT | Result: bigboxlist,big list item 1
Command: lMpop 1 bigboxlist LEFT count 2 | Result: bigboxlist,big list item 2,big list item 3
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: bigboxlist,big lits item 4,big list item 5
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: smallboxlist,small list item 1
Command: lMpop 1 nonexistinglist LEFT count 5 | Result: null
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 1
Command: lMpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: bigboxlist,item a
Command: set bigboxstr "My big box string" | Result: OK

Command: lMpop 1 bigboxstr right | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Command: lMpop 2 bigboxstr bigboxlist right | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Command: lMpop 2 bigboxlist bigboxstr right | Error:  [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]

Notes

  • Use the function “lmPop” from the package node-redis.
  • Here is the signature of the method –
    lmPop(keys: RedisCommandArgument | Array<RedisCommandArgument>, side: ListSide, options?: LMPopOptions)
  • Definition of the ListSide type is-
    type ListSide = ‘LEFT’ | ‘RIGHT’;
  • LMPopOptions is defined as-
    interface LMPopOptions {
        COUNT?: number;
    }
// Redis LMPOP command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListDirection;
import redis.clients.jedis.util.KeyValue;

import java.util.List;

public class Lmpop {
    public static void main(String[] args) {
        // Create connection pool
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Create list "bigboxlist" and push items
             *
             * Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
             * Result: (integer) 5
             */
            long rpushResult = jedis.rpush("bigboxlist", "big list item 1", "big list item 2", "big list item 3", "big lits item 4", "big list item 5");

            System.out.println("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: " + rpushResult);

            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "big list item 1"
             *      2) "big list item 2"
             *      3) "big list item 3"
             *      4) "big lits item 4"
             *      5) "big list item 5"
             */
            List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);

            System.out.println("Command: lrange bigboxlist 0 -1 | Result: " + lrangeResult.toString());

            /**
             * Create and push items in "smallboxlist"
             *
             * Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
             * Result: (integer) 3
             */
            rpushResult = jedis.rpush("smallboxlist", "small list item 1", "small list item 2", "small list item 3");

            System.out.println("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: " + rpushResult);

            /**
             * check item from list
             *
             * Command: lrange smallboxlist 0 -1
             * Result:
             *     1) "small list item 1"
             *     2) "small list item 2"
             *     3) "small list item 3"
             */
            lrangeResult = jedis.lrange("smallboxlist", 0, -1);

            System.out.println("Command: lrange smallboxlist 0 -1 | Result: " + lrangeResult.toString());

            /**
             * Use LMPOP on bigboxlist and pop item form left
             *
             * Command: lmpop 1 bigboxlist LEFT
             * Result:
             *     1) "bigboxlist"
             *     2) 1) "big list item 1"
             */
            KeyValue<String, List<String>> lmpopResult = jedis.lmpop(ListDirection.LEFT, "bigboxlist");

            System.out.println("Command: lmpop 1 bigboxlist LEFT | Result: " + lmpopResult.toString());


            /**
             * Pop 2 items from the LEFT of bigboxlist
             *
             * Command: lmpop 1 bigboxlist LEFT count 2
             * Result:
             *     1) "bigboxlist"
             *     2)      1) "big list item 2"
             *             2) "big list item 3"
             */
            lmpopResult = jedis.lmpop(ListDirection.LEFT, 2, "bigboxlist");

            System.out.println("Command: lmpop 1 bigboxlist LEFT count 2 | Result: " + lmpopResult.toString());

            /**
             * Try to pop items from any of bigboxlist or smallboxlist
             * Items popped from bigboxlist as this list still has item
             *
             * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
             * Result:
             *     1) "bigboxlist"
             *     2)      1) "big lits item 4"
             *             2) "big list item 5"
             */
            lmpopResult = jedis.lmpop(ListDirection.LEFT, 2, "bigboxlist", "smallboxlist");

            System.out.println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: " + lmpopResult.toString());

            /**
             * Try to pop again from any of bigbostlist or smallboxlist
             * Items poped from smallboxlist, as there is no item in bigboxlist
             *
             * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
             * Result:
             *     1) "smallboxlist"
             *     2)      1) "small list item 1"
             *             2) "small list item 2"
             *             3) "small list item 3"
             */
            lmpopResult = jedis.lmpop(ListDirection.LEFT, 5, "bigboxlist", "smallboxlist");

            System.out.println("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: " + lmpopResult.toString());

            /**
             * Try to pop from a non existing list
             * It returns (nil)
             *
             * Command: lmpop 1 nonexistinglist LEFT count 5
             * Result: (nil)
             */
            lmpopResult = jedis.lmpop(ListDirection.LEFT, 5, "nonexistinglist");

            System.out.println("Command: lmpop 1 nonexistinglist LEFT count 5 | Result: " + lmpopResult);

            /**
             * Push some items in bigboxlist for continuing the test
             * Command: rpush bigboxlist "item a" "item b" "item c" "item d"
             * Result: (integer) 4
             */
            rpushResult = jedis.rpush("bigboxlist", "item a", "item b", "item c", "item d", "item e", "item f", "item g", "item h");

            System.out.println("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: " + rpushResult);

            /**
             * Try to pop item from any of a non existing list or bigboxlist
             * items popped from bigboxlist and the non existing list is ignored
             * Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
             * Result:
             *         1) "bigboxlist"
             *         2)      1) "item a"
             *                 2) "item b"
             *                 3) "item c"
             *                 4) "item d"
             */
            lmpopResult = jedis.lmpop(ListDirection.LEFT, 5, "nonexistinglist", "bigboxlist");

            System.out.println("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: " + lmpopResult.toString());

            /**
             * Set a string value
             *
             * Command: set bigboxstr "My big box string"
             * Result: OK
             */
            String setResult = jedis.set("bigboxstr", "My big box string");

            System.out.println("Command: set bigboxstr "My big box string" | Result: " + setResult);

            /**
             * Try to pop from a string item
             * It returns an error
             * Command: lmpop 1 bigboxstr right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                lmpopResult = jedis.lmpop(ListDirection.RIGHT, "bigboxstr");

                System.out.println("Command: lmpop 1 bigboxstr right | Result: " + lmpopResult.toString());
            } catch (Exception e) {
                System.out.println("Command: lmpop 1 bigboxstr right | Error: " + e.getMessage());
            }


            /**
             * Try to pop items from a string and a list
             * we get an error as the string is the first item and the command tries to pop items from the string
             *
             * Command: lmpop 2 bigboxstr bigboxlist right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try {
                lmpopResult = jedis.lmpop(ListDirection.RIGHT, "bigboxstr", "bigboxlist");

                System.out.println("Command: lmpop 2 bigboxstr bigboxlist right | Result: " + lmpopResult.toString());
            } catch (Exception e) {
                System.out.println("Command: lmpop 2 bigboxstr bigboxlist right | Error: " + e.getMessage());
            }

            /**
             * Try to pop items from a list and string
             * we get data if the list is non empty
             *
             * Command: lmpop 2 bigboxlist bigboxstr right
             * Result:
             *      1) "bigboxlist"
             *      2)      1) "big list item 5"
             */
            try {
                lmpopResult = jedis.lmpop(ListDirection.RIGHT, "bigboxlist", "bigboxstr");

                System.out.println("Command: lmpop 2 bigboxlist bigboxstr right | Result: " + lmpopResult.toString());
            } catch (Exception e) {
                System.out.println("Command: lmpop 2 bigboxlist bigboxstr right | Error: " + e.getMessage());
            }
        }

        jedisPool.close();
    }
}

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: [big list item 1, big list item 2, big list item 3, big lits item 4, big list item 5]
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 3
Command: lrange smallboxlist 0 -1 | Result: [small list item 1, small list item 2, small list item 3]
Command: lmpop 1 bigboxlist LEFT | Result: bigboxlist=[big list item 1]
Command: lmpop 1 bigboxlist LEFT count 2 | Result: bigboxlist=[big list item 2, big list item 3]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: bigboxlist=[big lits item 4, big list item 5]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: smallboxlist=[small list item 1, small list item 2, small list item 3]
Command: lmpop 1 nonexistinglist LEFT count 5 | Result: null
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 8
Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: bigboxlist=[item a, item b, item c, item d, item e]
Command: set bigboxstr "My big box string" | Result: OK
Command: lmpop 1 bigboxstr right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxstr bigboxlist right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxlist bigboxstr right | Result: bigboxlist=[item h]

Notes

  • Use method “lmpop” from Jedis package.
  • This “lmpop” method has 2 variations. Signature of the methods are-
    public KeyValue<String, List<String>> lmpop(ListDirection direction, String… keys)
    public KeyValue<String, List<String>> lmpop(ListDirection direction, int count, String… keys)
// Redis LMPOP command examples in C#

using StackExchange.Redis;

namespace Lmpop
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
             * Create list "bigboxlist" and push items
             *
             * Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
             * Result: (integer) 5
             */
            long rpushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "big list item 1", "big list item 2", "big list item 3", "big lits item 4", "big list item 5" });

            Console.WriteLine("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: " + rpushResult);

            /**
             * Check list items
             *
             * Command: lrange bigboxlist 0 -1
             * Result:
             *      1) "big list item 1"
             *      2) "big list item 2"
             *      3) "big list item 3"
             *      4) "big lits item 4"
             *      5) "big list item 5"
             */
            RedisValue[] lrangeResult = rdb.ListRange("bigboxlist", 0, -1);

            Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result: " + string.Join(", ", lrangeResult));

            /**
             * Create and push items in "smallboxlist"
             *
             * Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
             * Result: (integer) 3
             */
            rpushResult = rdb.ListRightPush("smallboxlist", new RedisValue[] { "small list item 1", "small list item 2", "small list item 3" });

            Console.WriteLine("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: " + rpushResult);

            /**
             * check item from list
             *
             * Command: lrange smallboxlist 0 -1
             * Result:
             *     1) "small list item 1"
             *     2) "small list item 2"
             *     3) "small list item 3"
             */
            lrangeResult = rdb.ListRange("smallboxlist", 0, -1);

            Console.WriteLine("Command: lrange smallboxlist 0 -1 | Result: " + string.Join(", ", lrangeResult));


            /**
             * Use LMPOP on bigboxlist and pop item form left
             *
             * Command: lmpop 1 bigboxlist LEFT
             * Result:
             *     1) "bigboxlist"
             *     2) 1) "big list item 1"
             */
            var lmpopResult = rdb.ListLeftPop(new RedisKey[] { "bigboxlist" }, 1);

            Console.WriteLine("Command: lmpop 1 bigboxlist LEFT | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Pop 2 items from the LEFT of bigboxlist
             *
             * Command: lmpop 1 bigboxlist LEFT count 2
             * Result:
             *     1) "bigboxlist"
             *     2)      1) "big list item 2"
             *             2) "big list item 3"
             */
            lmpopResult = rdb.ListLeftPop(new RedisKey[] { "bigboxlist" }, 2);

            Console.WriteLine("Command: lmpop 1 bigboxlist LEFT count 2 | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Try to pop items from any of bigboxlist or smallboxlist
             * Items popped from bigboxlist as this list still has item
             *
             * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
             * Result:
             *     1) "bigboxlist"
             *     2)      1) "big lits item 4"
             *             2) "big list item 5"
             */
            lmpopResult = rdb.ListLeftPop(new RedisKey[] { "bigboxlist", "smallboxlist" }, 2);

            Console.WriteLine("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Try to pop again from any of bigbostlist or smallboxlist
             * Items poped from smallboxlist, as there is no item in bigboxlist
             *
             * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
             * Result:
             *     1) "smallboxlist"
             *     2)      1) "small list item 1"
             *             2) "small list item 2"
             *             3) "small list item 3"
             */
            lmpopResult = rdb.ListLeftPop(new RedisKey[] { "bigboxlist", "smallboxlist" }, 5);

            Console.WriteLine("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Try to pop from a non existing list
             * It returns (nil)
             *
             * Command: lmpop 1 nonexistinglist LEFT count 5
             * Result: (nil)
             */
            lmpopResult = rdb.ListLeftPop(new RedisKey[] { "nonexistinglist" }, 5);

            Console.WriteLine("Command: lmpop 1 nonexistinglist LEFT count 5 | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Push some items in bigboxlist for continuing the test
             * Command: rpush bigboxlist "item a" "item b" "item c" "item d"
             * Result: (integer) 4
             */
            rpushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "item a", "item b", "item c", "item d", "item e", "item f", "item g", "item h" });

            Console.WriteLine("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: " + rpushResult);

            /**
             * Try to pop item from any of a non existing list or bigboxlist
             * items popped from bigboxlist and the non existing list is ignored
             * Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
             * Result:
             *         1) "bigboxlist"
             *         2)      1) "item a"
             *                 2) "item b"
             *                 3) "item c"
             *                 4) "item d"
             */
            lmpopResult = rdb.ListLeftPop(new RedisKey[] { "nonexistinglist", "bigboxlist" }, 5);

            Console.WriteLine("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));

            /**
             * Set a string value
             *
             * Command: set bigboxstr "My big box string"
             * Result: OK
             */
            bool setResult = rdb.StringSet("bigboxstr", "My big box string");

            Console.WriteLine("Command: set bigboxstr "My big box string" | Result: " + setResult);

            /**
             * Try to pop from a string item
             * It returns an error
             * Command: lmpop 1 bigboxstr right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                lmpopResult = rdb.ListRightPop(new RedisKey[] { "bigboxstr" }, 1);

                Console.WriteLine("Command: lmpop 1 bigboxstr right | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lmpop 1 bigboxstr right | Error: " + e.Message);
            }


            /**
             * Try to pop items from a string and a list
             * we get an error as the string is the first item and the command tries to pop items from the string
             *
             * Command: lmpop 2 bigboxstr bigboxlist right
             * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
             */
            try
            {
                lmpopResult = rdb.ListRightPop(new RedisKey[] { "bigboxstr", "bigboxlist" }, 1);

                Console.WriteLine("Command: lmpop 2 bigboxstr bigboxlist right | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lmpop 2 bigboxstr bigboxlist right | Error: " + e.Message);
            }

            /**
             * Try to pop items from a list and string
             * we get data if the list is non empty
             *
             * Command: lmpop 2 bigboxlist bigboxstr right
             * Result:
             *      1) "bigboxlist"
             *      2)      1) "big list item 5"
             */
            try
            {
                lmpopResult = rdb.ListRightPop(new RedisKey[] { "bigboxlist", "bigboxstr" }, 1);

                Console.WriteLine("Command: lmpop 2 bigboxlist bigboxstr right | Result: key={0} || value: {1}", lmpopResult.Key, string.Join(",", lmpopResult.Values));
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: lmpop 2 bigboxlist bigboxstr right | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: big list item 1, big list item 2, big list item 3, big lits item 4, big list item 5
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 3
Command: lrange smallboxlist 0 -1 | Result: small list item 1, small list item 2, small list item 3

Command: lmpop 1 bigboxlist LEFT | Result: key=bigboxlist || value: big list item 1
Command: lmpop 1 bigboxlist LEFT count 2 | Result: key=bigboxlist || value: big list item 2,big list item 3
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: key=bigboxlist || value: big lits item 4,big list item 5
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: key=smallboxlist || value: small list item 1,small list item 2,small list item 3
Command: lmpop 1 nonexistinglist LEFT count 5 | Result: key=(null) || value:
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 8
Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: key=bigboxlist || value: item a,item b,item c,item d,item e

Command: set bigboxstr "My big box string" | Result: True

Command: lmpop 1 bigboxstr right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxstr bigboxlist right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxlist bigboxstr right | Result: key=bigboxlist || value: item h

Notes

  • Use the methods “ListLeftPop“, “ListRightPop” from StackExchange.Redis.
  • Signature of the method for LMPOP is as below –
    ListPopResult ListLeftPop(RedisKey[] keys, long count, CommandFlags flags = CommandFlags.None)
    ListPopResult ListRightPop(RedisKey[] keys, long count, CommandFlags flags = CommandFlags.None)
<?php
// Redis LMPOP command example in PHP

require 'vendor/autoload.php';

// Connect to Redis
$redisClient = new PredisClient([
    'scheme' => 'tcp',
    'host' => 'localhost',
    'port' => 6379,
]);


/**
 * Create list "bigboxlist" and push items
 *
 * Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
 * Result: (integer) 5
 */
$commandResult = $redisClient->rpush("bigboxlist", [
    "big list item 1",
    "big list item 2",
    "big list item 3",
    "big lits item 4",
    "big list item 5",
]);

echo 'Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: ' . $commandResult . "n";

/**
 * Check list items
 *
 * Command: lrange bigboxlist 0 -1
 * Result:
 *      1) "big list item 1"
 *      2) "big list item 2"
 *      3) "big list item 3"
 *      4) "big lits item 4"
 *      5) "big list item 5"
 */
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);

echo "Command: lrange bigboxlist 0 -1 | Result: ";
print_r($commandResult);

/**
 * Create and push items in "smallboxlist"
 *
 * Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
 * Result: (integer) 3
 */
$commandResult = $redisClient->rPush("smallboxlist", [
    "small list item 1",
    "small list item 2",
    "small list item 3"
]);

echo 'Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: ' . $commandResult . "n";

/**
 * check item from list
 *
 * Command: lrange smallboxlist 0 -1
 * Result:
 *     1) "small list item 1"
 *     2) "small list item 2"
 *     3) "small list item 3"
 */
$commandResult = $redisClient->lrange("smallboxlist", 0, -1);

echo "Command: lrange smallboxlist 0 -1 | Result: ";
print_r($commandResult);


/**
 * Use LMPOP on bigboxlist and pop item form left
 *
 * Command: lmpop 1 bigboxlist LEFT
 * Result:
 *     1) "bigboxlist"
 *     2) 1) "big list item 1"
 */
$commandResult = $redisClient->lmpop(["bigboxlist"], "left");

echo "Command: lmpop 1 bigboxlist LEFT | Result: ";
print_r($commandResult);

/**
 * Pop 2 items from the LEFT of bigboxlist
 *
 * Command: lmpop 1 bigboxlist LEFT count 2
 * Result:
 *     1) "bigboxlist"
 *     2)      1) "big list item 2"
 *             2) "big list item 3"
 */
$commandResult = $redisClient->lmpop(["bigboxlist"], "left", 2);

echo "Command: lmpop 1 bigboxlist LEFT count 2 | Result: ";
print_r($commandResult);

/**
 * Try to pop items from any of bigboxlist or smallboxlist
 * Items popped from bigboxlist as this list still has item
 *
 * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
 * Result:
 *     1) "bigboxlist"
 *     2)      1) "big lits item 4"
 *             2) "big list item 5"
 */
$commandResult = $redisClient->lmpop(["bigboxlist", "smallboxlist"], "left", 2);

echo "Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ";
print_r($commandResult);

/**
 * Try to pop again from any of bigbostlist or smallboxlist
 * Items poped from smallboxlist, as there is no item in bigboxlist
 *
 * Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
 * Result:
 *     1) "smallboxlist"
 *     2)      1) "small list item 1"
 *             2) "small list item 2"
 *             3) "small list item 3"
 */
$commandResult = $redisClient->lmpop(["bigboxlist", "smallboxlist"], "left", 5);

echo "Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ";
print_r($commandResult);

/**
 * Try to pop from a non existing list
 * It returns (nil)
 *
 * Command: lmpop 1 nonexistinglist LEFT count 5
 * Result: (nil)
 */
$commandResult = $redisClient->lmpop(["nonexistinglist"], "left", 5);

echo "Command: lmpop 1 nonexistinglist LEFT count 5 | Result: ";
print_r($commandResult);

/**
 * Push some items in bigboxlist for continuing the test
 * Command: rpush bigboxlist "item a" "item b" "item c" "item d"
 * Result: (integer) 4
 */
$commandResult = $redisClient->rpush("bigboxlist", [
    "item a",
    "item b",
    "item c",
    "item d",
    "item e",
    "item f",
    "item g",
    "item h"
]);

echo 'Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: ' . $commandResult . "n";

/**
 * Try to pop item from any of a non existing list or bigboxlist
 * items popped from bigboxlist and the non existing list is ignored
 * Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
 * Result:
 *         1) "bigboxlist"
 *         2)      1) "item a"
 *                 2) "item b"
 *                 3) "item c"
 *                 4) "item d"
 */
$commandResult = $redisClient->lmpop(["nonexistinglist", "bigboxlist"], "left", 5);

echo "Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: ";
print_r($commandResult);

/**
 * Set a string value
 *
 * Command: set bigboxstr "My big box string"
 * Result: OK
 */
$commandResult = $redisClient->set("bigboxstr", "My big box string");

echo 'Command: set bigboxstr "My big box string" | Result: ' . $commandResult . "n";

/**
 * Try to pop from a string item
 * It returns an error
 * Command: lmpop 1 bigboxstr right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->lmpop(["bigboxstr"], "left");

    echo "Command: lmpop 1 bigboxstr right | Result: ";
    print_r($commandResult);
} catch (Exception $e) {
    echo "Command: lmpop 1 bigboxstr right | Error: " . $e->getMessage() . "n";
}

/**
 * Try to pop items from a string and a list
 * we get an error as the string is the first item and the command tries to pop items from the string
 *
 * Command: lmpop 2 bigboxstr bigboxlist right
 * Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
 */
try {
    $commandResult = $redisClient->lmpop(["bigboxstr", "bigboxlist"], "right");

    echo "Command: lmpop 2 bigboxstr bigboxlist right | Result: ";
    print_r($commandResult);
} catch (Exception $e) {
    echo "Command: lmpop 2 bigboxstr bigboxlist right | Error: " . $e->getMessage() . "n";
}

/**
 * Try to pop items from a list and string
 * we get data if the list is non empty
 *
 * Command: lmpop 2 bigboxlist bigboxstr right
 * Result:
 *      1) "bigboxlist"
 *      2)      1) "big list item 5"
 */
try {
    $commandResult = $redisClient->lmpop(["bigboxlist", "bigboxstr"], "right");

    echo "Command: lmpop 2 bigboxlist bigboxstr right | Result: ";
    print_r($commandResult);
} catch (Exception $e) {
    echo "Command: lmpop 2 bigboxlist bigboxstr right | Error: " . $e->getMessage() . "n";
}

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: Array
(
    [0] => big list item 1
    [1] => big list item 2
    [2] => big list item 3
    [3] => big lits item 4
    [4] => big list item 5
)
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 3
Command: lrange smallboxlist 0 -1 | Result: Array
(
    [0] => small list item 1
    [1] => small list item 2
    [2] => small list item 3
)


Command: lMpop 1 bigboxlist LEFT | Result: Array
(
    [bigboxlist] => Array
        (
            [0] => big list item 1
        )

)
Command: lMpop 1 bigboxlist LEFT count 2 | Result: Array
(
    [bigboxlist] => Array
        (
            [0] => big list item 2
            [1] => big list item 3
        )

)
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: Array
(
    [bigboxlist] => Array
        (
            [0] => big lits item 4
            [1] => big list item 5
        )

)
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: Array
(
    [smallboxlist] => Array
        (
            [0] => small list item 1
            [1] => small list item 2
            [2] => small list item 3
        )

)
Command: lMpop 1 nonexistinglist LEFT count 5 | Result: Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 8   
Command: lMpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: Array
(
    [bigboxlist] => Array
        (
            [0] => item a
            [1] => item b
            [2] => item c
            [3] => item d
            [4] => item e
        )

)

Command: set bigboxstr "My big box string" | Result: OK

Command: lMpop 1 bigboxstr right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lMpop 2 bigboxstr bigboxlist right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lMpop 2 bigboxlist bigboxstr right | Result: Array
(
    [bigboxlist] => Array
        (
            [0] => item h
        )

)

Notes

  • Use the method “lmpop” of predis.
  • Signature of the method is-
    lmpop(array $keys, string $modifier = ‘left’, int $count = 1): array|null
  • Make sure to pass the modifier left/right in lower case.
# Redis LMPOP command example in Python

import redis
import time

# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
                          username='default', password='',
                          decode_responses=True)


# Create list "bigboxlist" and push items
# Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
# Result: (integer) 5
commandResult = redisClient.rpush("bigboxlist",
    "big list item 1",
    "big list item 2",
    "big list item 3",
    "big lits item 4",
    "big list item 5",
)

print("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: {}".format(commandResult))

# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
#      1) "big list item 1"
#      2) "big list item 2"
#      3) "big list item 3"
#      4) "big lits item 4"
#      5) "big list item 5"
commandResult = redisClient.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result: {}".format(commandResult))

# Create and push items in "smallboxlist"
# Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
# Result: (integer) 3
commandResult = redisClient.rpush("smallboxlist", 
    "small list item 1",
    "small list item 2",
    "small list item 3"
)

print("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: {}".format(commandResult))

# check item from list
# Command: lrange smallboxlist 0 -1
# Result:
#     1) "small list item 1"
#     2) "small list item 2"
#     3) "small list item 3"
commandResult = redisClient.lrange("smallboxlist", 0, -1)

print("Command: lrange smallboxlist 0 -1 | Result: {}".format(commandResult))

# Use LMPOP on bigboxlist and pop item form left
# Command: lmpop 1 bigboxlist LEFT
# Result:
#     1) "bigboxlist"
#     2) 1) "big list item 1"
commandResult = redisClient.lmpop(1, "bigboxlist", direction="left")

print("Command: lmpop 1 bigboxlist LEFT | Result: {}".format(commandResult))

# Pop 2 items from the LEFT of bigboxlist
# Command: lmpop 1 bigboxlist LEFT count 2
# Result:
#     1) "bigboxlist"
#     2)      1) "big list item 2"
#             2) "big list item 3"
commandResult = redisClient.lmpop(1, "bigboxlist", direction="left", count=2)

print("Command: lmpop 1 bigboxlist LEFT count 2 | Result: {}".format(commandResult))

# Try to pop items from any of bigboxlist or smallboxlist
# Items popped from bigboxlist as this list still has item
# Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
# Result:
#     1) "bigboxlist"
#     2)      1) "big lits item 4"
#             2) "big list item 5"
commandResult = redisClient.lmpop(2, "bigboxlist", "smallboxlist", direction="left", count=2)

print("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: {}".format(commandResult))

# Try to pop again from any of bigbostlist or smallboxlist
# Items poped from smallboxlist, as there is no item in bigboxlist
# Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
# Result:
#     1) "smallboxlist"
#     2)      1) "small list item 1"
#             2) "small list item 2"
#             3) "small list item 3"
commandResult = redisClient.lmpop(2, "bigboxlist", "smallboxlist", direction="left", count=5)

print("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: {}".format(commandResult))

# Try to pop from a non existing list
# It returns (nil)
# Command: lmpop 1 nonexistinglist LEFT count 5
# Result: (nil)
commandResult = redisClient.lmpop(1, "nonexistinglist", direction="left", count=5)

print("Command: lmpop 1 nonexistinglist LEFT count 5 | Result: {}".format(commandResult))

# Push some items in bigboxlist for continuing the test
# Command: rpush bigboxlist "item a" "item b" "item c" "item d"
# Result: (integer) 4
commandResult = redisClient.rpush("bigboxlist",
    "item a",
    "item b",
    "item c",
    "item d",
    "item e",
    "item f",
    "item g",
    "item h"
)

print("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: {}".format(commandResult))

# Try to pop item from any of a non existing list or bigboxlist
# items popped from bigboxlist and the non existing list is ignored
# Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
# Result:
#         1) "bigboxlist"
#         2)      1) "item a"
#                 2) "item b"
#                 3) "item c"
#                 4) "item d"
commandResult = redisClient.lmpop(2, "nonexistinglist", "bigboxlist", direction="left", count=5)

print("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: {}".format(commandResult))

# Set a string value
# Command: set bigboxstr "My big box string"
# Result: OK
commandResult = redisClient.set("bigboxstr", "My big box string")

print("Command: set bigboxstr "My big box string" | Result: {}".format(commandResult))

# Try to pop from a string item
# It returns an error
# Command: lmpop 1 bigboxstr right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.lmpop(1, "bigboxstr", direction="left")

    print("Command: lmpop 1 bigboxstr right | Result: {}".format(commandResult))
except Exception as error:
    print("Command: lmpop 1 bigboxstr right | Error: ", error)

# Try to pop items from a string and a list
# we get an error as the string is the first item and the command tries to pop items from the string
# Command: lmpop 2 bigboxstr bigboxlist right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
    commandResult = redisClient.lmpop(2, "bigboxstr", "bigboxlist", direction="right")

    print("Command: lmpop 2 bigboxstr bigboxlist right | Result: {}".format(commandResult))
except Exception as error:
    print("Command: lmpop 2 bigboxstr bigboxlist right | Error: ", error)

# Try to pop items from a list and string
# we get data if the list is non empty
# Command: lmpop 2 bigboxlist bigboxstr right
# Result:
#      1) "bigboxlist"
#      2)      1) "big list item 5"
try:
    commandResult = redisClient.lmpop(2, "bigboxlist", "bigboxstr", direction="right")

    print("Command: lmpop 2 bigboxlist bigboxstr right | Result: {}".format(commandResult))
except Exception as error:
    print("Command: lmpop 2 bigboxlist bigboxstr right | Error: ", error)

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: ['big list item 1', 'big list item 2', 'big list item 3', 'big lits item 4', 'big list item 5']
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 3
Command: lrange smallboxlist 0 -1 | Result: ['small list item 1', 'small list item 2', 'small list item 3']

Command: lMpop 1 bigboxlist LEFT | Result: ['bigboxlist', ['big list item 1']]
Command: lMpop 1 bigboxlist LEFT count 2 | Result: ['bigboxlist', ['big list item 2', 'big list item 3']]
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ['bigboxlist', ['big lits item 4', 'big list item 5']]
Command: lMpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ['smallboxlist', ['small list item 1', 'small list item 2', 'small list item 3']]
Command: lMpop 1 nonexistinglist LEFT count 5 | Result: None
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 8
Command: lMpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: ['bigboxlist', ['item a', 'item b', 'item c', 'item d', 'item e']]

Command: set bigboxstr "My big box string" | Result: True
Command: lMpop 1 bigboxstr right | Error:  WRONGTYPE Operation against a key holding the wrong kind of value

Command: lMpop 2 bigboxstr bigboxlist right | Error:  WRONGTYPE Operation against a key holding the wrong kind of value
Command: lMpop 2 bigboxlist bigboxstr right | Result: ['bigboxlist', ['item h']]

Notes

  • Use method “lmpop” from redis-py.
  • Signature of the method is –
    def lmpop(self, num_keys: int, *args: List[str], direction: str, count: Optional[int] = 1,) -> Union[Awaitable[list], list]
# Redis LMPOP command example in Ruby

require 'redis'

redis = Redis.new(host: "localhost", port: 6379)


# Create list "bigboxlist" and push items
# Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5"
# Result: (integer) 5
commandResult = redis.rpush("bigboxlist",[
    "big list item 1",
    "big list item 2",
    "big list item 3",
    "big lits item 4",
    "big list item 5",
])

print("Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: ", commandResult, "n")

# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
#      1) "big list item 1"
#      2) "big list item 2"
#      3) "big list item 3"
#      4) "big lits item 4"
#      5) "big list item 5"
commandResult = redis.lrange("bigboxlist", 0, -1)

print("Command: lrange bigboxlist 0 -1 | Result: ", commandResult, "n")

# Create and push items in "smallboxlist"
# Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3"
# Result: (integer) 3
commandResult = redis.rpush("smallboxlist", [
    "small list item 1",
    "small list item 2",
    "small list item 3"
])

print("Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: ", commandResult, "n")

# check item from list
# Command: lrange smallboxlist 0 -1
# Result:
#     1) "small list item 1"
#     2) "small list item 2"
#     3) "small list item 3"
commandResult = redis.lrange("smallboxlist", 0, -1)

print("Command: lrange smallboxlist 0 -1 | Result: ", commandResult, "n")

# Use LMPOP on bigboxlist and pop item form left
# Command: lmpop 1 bigboxlist LEFT
# Result:
#     1) "bigboxlist"
#     2) 1) "big list item 1"
commandResult = redis.lmpop("bigboxlist", "left")

print("Command: lmpop 1 bigboxlist LEFT | Result: ", commandResult, "n")

# Pop 2 items from the LEFT of bigboxlist
# Command: lmpop 1 bigboxlist LEFT count 2
# Result:
#     1) "bigboxlist"
#     2)      1) "big list item 2"
#             2) "big list item 3"
commandResult = redis.lmpop("bigboxlist", "left", 2)

print("Command: lmpop 1 bigboxlist LEFT count 2 | Result: ", commandResult, "n")

# Try to pop items from any of bigboxlist or smallboxlist
# Items popped from bigboxlist as this list still has item
# Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
# Result:
#     1) "bigboxlist"
#     2)      1) "big lits item 4"
#             2) "big list item 5"
commandResult = redis.lmpop("bigboxlist", "smallboxlist", "left", 2)

print("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ", commandResult, "n")

# Try to pop again from any of bigbostlist or smallboxlist
# Items poped from smallboxlist, as there is no item in bigboxlist
# Command: lmpop 2 bigboxlist smallboxlist LEFT count 5
# Result:
#     1) "smallboxlist"
#     2)      1) "small list item 1"
#             2) "small list item 2"
#             3) "small list item 3"
commandResult = redis.lmpop("bigboxlist", "smallboxlist", "left", 5)

print("Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ", commandResult, "n")

# Try to pop from a non existing list
# It returns (nil)
# Command: lmpop 1 nonexistinglist LEFT count 5
# Result: (nil)
commandResult = redis.lmpop("nonexistinglist", "left", 5)

print("Command: lmpop 1 nonexistinglist LEFT count 5 | Result: ", commandResult, "n")

# Push some items in bigboxlist for continuing the test
# Command: rpush bigboxlist "item a" "item b" "item c" "item d"
# Result: (integer) 4
commandResult = redis.rpush("bigboxlist", [
    "item a",
    "item b",
    "item c",
    "item d",
    "item e",
    "item f",
    "item g",
    "item h"
])

print("Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: ", commandResult, "n")

# Try to pop item from any of a non existing list or bigboxlist
# items popped from bigboxlist and the non existing list is ignored
# Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5
# Result:
#         1) "bigboxlist"
#         2)      1) "item a"
#                 2) "item b"
#                 3) "item c"
#                 4) "item d"
commandResult = redis.lmpop("nonexistinglist", "bigboxlist", "left", 5)

print("Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: ", commandResult, "n")

# Set a string value
# Command: set bigboxstr "My big box string"
# Result: OK
commandResult = redis.set("bigboxstr", "My big box string")

print("Command: set bigboxstr "My big box string" | Result: ", commandResult, "n")

# Try to pop from a string item
# It returns an error
# Command: lmpop 1 bigboxstr right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.lmpop("bigboxstr", "left")

    print("Command: lmpop 1 bigboxstr right | Result: ", commandResult, "n")
rescue => e
    print("Command: lmpop 1 bigboxstr right | Error: ", e, "n")
end

# Try to pop items from a string and a list
# we get an error as the string is the first item and the command tries to pop items from the string
# Command: lmpop 2 bigboxstr bigboxlist right
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
    commandResult = redis.lmpop("bigboxstr", "bigboxlist", "right")

    print("Command: lmpop 2 bigboxstr bigboxlist right | Result: ", commandResult, "n")
rescue => e
    print("Command: lmpop 2 bigboxstr bigboxlist right | Error: ", e, "n")
end

# Try to pop items from a list and string
# we get data if the list is non empty
# Command: lmpop 2 bigboxlist bigboxstr right
# Result:
#      1) "bigboxlist"
#      2)      1) "big list item 5"
begin
    commandResult = redis.lmpop("bigboxlist", "bigboxstr", "right")

    print("Command: lmpop 2 bigboxlist bigboxstr right | Result: ", commandResult, "n")
rescue => e
    print("Command: lmpop 2 bigboxlist bigboxstr right | Error: ", e, "n")
end

Output:

Command: rpush bigboxlist "big list item 1" "big list item 2" "big list item 3" "big lits item 4" "big list item 5" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: ["big list item 1", "big list item 2", "big list item 3", "big lits item 4", "big list item 5"]
Command: rpush smallboxlist "small list item 1" "small list item 2" "small list item 3" | Result: 3
Command: lrange smallboxlist 0 -1 | Result: ["small list item 1", "small list item 2", "small list item 3"]

Command: lmpop 1 bigboxlist LEFT | Result: ["bigboxlist", ["big list item 1"]]
Command: lmpop 1 bigboxlist LEFT count 2 | Result: ["bigboxlist", ["big list item 2"]]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ["bigboxlist", ["big list item 3"]]
Command: lmpop 2 bigboxlist smallboxlist LEFT count 5 | Result: ["bigboxlist", ["big lits item 4"]]
Command: lmpop 1 nonexistinglist LEFT count 5 | Result: 
Command: rpush bigboxlist "item a" "item b" "item c" "item d" | Result: 9
Command: lmpop 2 nonexistinglist bigboxlist LEFT count 5 | Result: ["bigboxlist", ["big list item 5"]]

Command: set bigboxstr "My big box string" | Result: OK
Command: lmpop 1 bigboxstr right | Error: WRONGTYPE Operation against a key holding the wrong kind of value

Command: lmpop 2 bigboxstr bigboxlist right | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lmpop 2 bigboxlist bigboxstr right | Result: ["bigboxlist", ["item a"]]

Notes

  • Use method “lmpop” from the redis-rb.
  • Signature of the method is-
    def lmpop(*keys, modifier: “LEFT”, count: nil)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
LPUSH Command Details
RPUSH Command Details
LRANGE Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.