LPOP

LPOP key [count]
Available since:
Redis Open Source 1.0.0
Time complexity:
O(N) where N is the number of elements returned
ACL categories:
@write, @list, @fast,
Compatibility:
Redis Software and Redis Cloud compatibility

Removes and returns the first elements of the list stored at key.

By default, the command pops a single element from the beginning of the list. When provided with the optional count argument, the reply will consist of up to count elements, depending on the list's length.

Required arguments

key

The name of the key that holds the list.

Optional arguments

count

The number of elements to pop. Without it, a single element is popped.

Examples

Foundational: Remove and return the first element(s) from a list using LPOP (supports optional count parameter to pop multiple elements)
redis> RPUSH mylist "one" "two" "three" "four" "five"
(integer) 5
redis> LPOP mylist
"one"
redis> LPOP mylist 2
1) "two"
2) "three"
redis> LRANGE mylist 0 -1
1) "four"
2) "five"
res17 = r.rpush("mylist", *["one", "two", "three", "four", "five"])
print(res17) # >>> 5

res18 = r.lpop("mylist")
print(res18) # >>> "one"

res19 = r.lpop("mylist", 2)
print(res19) # >>> ['two', 'three']

res17 = r.lrange("mylist", 0, -1)
print(res17)  # >>> [ "four", "five" ]

const res17 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
console.log(res17); // 5

const res18 = await client.lPop('mylist');
console.log(res18); // 'one'

const res19 = await client.lPopCount('mylist', 2);
console.log(res19); // [ 'two', 'three' ]

const res20 = await client.lRange('mylist', 0, -1);
console.log(res20); // [ 'four', 'five' ]

        long lPopResult1 = jedis.rpush(
            "mylist", "one", "two", "three", "four", "five"
        );
        System.out.println(lPopResult1); // >>> 5

        String lPopResult2 = jedis.lpop("mylist");
        System.out.println(lPopResult2); // >>> one

        List<String> lPopResult3 = jedis.lpop("mylist", 2);
        System.out.println(lPopResult3); // >>> [two, three]

        List<String> lPopResult4 = jedis.lrange("mylist", 0, -1);
        System.out.println(lPopResult4); // >>> [four, five]
            CompletableFuture<Void> lpop = asyncCommands.rpush("mylist", "one", "two", "three", "four", "five")
                    .thenCompose(res17 -> {
                        System.out.println(res17); // >>> 5

                        return asyncCommands.lpop("mylist");
                    }).thenCompose(res18 -> {
                        System.out.println(res18); // >>> one

                        return asyncCommands.lpop("mylist", 2);
                    }).thenCompose(res19 -> {
                        System.out.println(res19); // >>> [two, three]

                        return asyncCommands.lrange("mylist", 0, -1);
                    })
                    .thenAccept(res17_final -> System.out.println(res17_final)) // >>> [four, five]
                    .toCompletableFuture();
            Mono<Void> lpop = reactiveCommands.rpush("mylist", "one", "two", "three", "four", "five").doOnNext(res17 -> {
                System.out.println(res17); // >>> 5
            }).flatMap(res17 -> reactiveCommands.lpop("mylist")).doOnNext(res18 -> {
                System.out.println(res18); // >>> one
            }).flatMap(res18 -> reactiveCommands.lpop("mylist", 2).collectList()).doOnNext(res19 -> {
                System.out.println(res19); // >>> [two, three]
            }).flatMap(res19 -> reactiveCommands.lrange("mylist", 0, -1).collectList()).doOnNext(res17_final -> {
                System.out.println(res17_final); // >>> [four, five]
            }).then();
	RPushResult, err := rdb.RPush(ctx,
		"mylist", "one", "two", "three", "four", "five",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(RPushResult) // >>> 5

	lPopResult, err := rdb.LPop(ctx, "mylist").Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lPopResult) // >>> one

	lPopCountResult, err := rdb.LPopCount(ctx, "mylist", 2).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lPopCountResult) // >>> [two three]

	lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult) // >>> [four five]
        long lPopResult1 = db.ListRightPush("mylist", ["one", "two", "three", "four", "five"]);
        Console.WriteLine(lPopResult1); // >>> 5

        RedisValue lPopResult2 = db.ListLeftPop("mylist");
        Console.WriteLine(lPopResult2); // >>> one

        RedisValue[] lPopResult3 = db.ListLeftPop("mylist", 2);
        Console.WriteLine($"[{string.Join(", ", lPopResult3)}]");
        // >>> [two, three]

        RedisValue[] lPopResult4 = db.ListRange("mylist", 0, -1);
        Console.WriteLine($"[{string.Join(", ", lPopResult4)}]");
        // >>> [four, five]
        $res17 = $r->rpush('mylist', 'one', 'two', 'three', 'four', 'five');
        echo $res17 . PHP_EOL;
        // >>> 5

        $res18 = $r->lpop('mylist');
        echo $res18 . PHP_EOL;
        // >>> one

        $res19 = $r->lpop('mylist', 2);
        echo json_encode($res19) . PHP_EOL;
        // >>> ["two","three"]

        $res20 = $r->lrange('mylist', 0, -1);
        echo json_encode($res20) . PHP_EOL;
        // >>> ["four","five"]

Give these commands a try in the interactive console:

RPUSH mylist "one" "two" "three" "four" "five" LPOP mylist LPOP mylist 2 LRANGE mylist 0 -1

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active

Return information

One of the following:

  • Nil reply: if the key does not exist.
  • Bulk string reply: when called without the count argument, the value of the first element.
  • Array reply: when called with the count argument, a list of popped elements.

History

  • Starting with Redis version 6.2.0: Added the count argument.
RATE THIS PAGE
Back to top ↑