LPOP
Syntax
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
,
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.
Examples
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"
Are you tired of using redis-cli? Try Redis Insight - the developer GUI for Redis.
import redis
r = redis.Redis(decode_responses=True)
res1 = r.lpush("mylist", "world")
print(res1) # >>> 1
res2 = r.lpush("mylist", "hello")
print(res2) # >>> 2
res3 = r.lrange("mylist", 0, -1)
print(res3) # >>> [ "hello", "world" ]
res4 = r.rpush("mylist", "one");
print(res4) # >>> 1
res5 = r.rpush("mylist", "two")
print(res5) # >>> 2
res6 = r.rpush("mylist", "three")
print(res6) # >>> 3
res7 = r.lrange('mylist', 0, 0)
print(res7) # >>> [ 'one' ]
res8 = r.lrange('mylist', -3, 2)
print(res8) # >>> [ 'one', 'two', 'three' ]
res9 = r.lrange('mylist', -100, 100)
print(res9) # >>> [ 'one', 'two', 'three' ]
res10 = r.lrange('mylist', 5, 10)
print(res10) # >>> []
res11 = r.lpush("mylist", "World")
print(res11) # >>> 1
res12 = r.lpush("mylist", "Hello")
print(res12) # >>> 2
res13 = r.llen("mylist")
print(res13) # >>> 2
res14 = r.rpush("mylist", "hello")
print(res14) # >>> 1
res15 = r.rpush("mylist", "world")
print(res15) # >>> 2
res16 = r.lrange("mylist", 0, -1)
print(res16) # >>> [ "hello", "world" ]
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" ]
res18 = r.rpush("mylist", *["one", "two", "three", "four", "five"])
print(res18) # >>> 5
res19 = r.rpop("mylist")
print(res19) # >>> "five"
res20 = r.rpop("mylist", 2)
print(res20) # >>> ['four', 'three']
res21 = r.lrange("mylist", 0, -1)
print(res21) # >>> [ "one", "two" ]
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const res1 = await client.lPush('mylist', 'world');
console.log(res1); // 1
const res2 = await client.lPush('mylist', 'hello');
console.log(res2); // 2
const res3 = await client.lRange('mylist', 0, -1);
console.log(res3); // [ 'hello', 'world' ]
const res4 = await client.rPush('mylist', 'one');
console.log(res4); // 1
const res5 = await client.rPush('mylist', 'two');
console.log(res5); // 2
const res6 = await client.rPush('mylist', 'three');
console.log(res6); // 3
const res7 = await client.lRange('mylist', 0, 0);
console.log(res7); // [ 'one' ]
const res8 = await client.lRange('mylist', -3, 2);
console.log(res8); // [ 'one', 'two', 'three' ]
const res9 = await client.lRange('mylist', -100, 100);
console.log(res9); // [ 'one', 'two', 'three' ]
const res10 = await client.lRange('mylist', 5, 10);
console.log(res10); // []
const res11 = await client.lPush('mylist', 'World');
console.log(res11); // 1
const res12 = await client.lPush('mylist', 'Hello');
console.log(res12); // 2
const res13 = await client.lLen('mylist');
console.log(res13); // 2
const res14 = await client.rPush('mylist', 'hello');
console.log(res14); // 1
const res15 = await client.rPush('mylist', 'world');
console.log(res15); // 2
const res16 = await client.lRange('mylist', 0, -1);
console.log(res16); // [ 'hello', 'world' ]
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' ]
const res21 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
console.log(res21); // 5
const res22 = await client.rPop('mylist');
console.log(res22); // 'five'
const res23 = await client.rPopCount('mylist', 2);
console.log(res23); // [ 'four', 'three' ]
const res24 = await client.lRange('mylist', 0, -1);
console.log(res24); // [ 'one', 'two' ]
await client.quit();
import java.util.List;
import redis.clients.jedis.UnifiedJedis;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CmdsListExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
long lLenResult1 = jedis.lpush("mylist", "World");
System.out.println(lLenResult1); // >>> 1
long lLenResult2 = jedis.lpush("mylist", "Hello");
System.out.println(lLenResult2); // >>> 2
long lLenResult3 = jedis.llen("mylist");
System.out.println(lLenResult3); // >>> 2
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]
long lPushResult1 = jedis.lpush("mylist", "World");
System.out.println(lPushResult1); // >>> 1
long lPushResult2 = jedis.lpush("mylist", "Hello");
System.out.println(lPushResult2); // >>> 2
List<String> lPushResult3 = jedis.lrange("mylist", 0, -1);
System.out.println(lPushResult3);
// >>> [Hello, World]
long lRangeResult1 = jedis.rpush("mylist", "one", "two", "three");
System.out.println(lRangeResult1); // >>> 3
List<String> lRangeResult2 = jedis.lrange("mylist", 0, 0);
System.out.println(lRangeResult2); // >>> [one]
List<String> lRangeResult3 = jedis.lrange("mylist", -3, 2);
System.out.println(lRangeResult3); // >>> [one, two, three]
List<String> lRangeResult4 = jedis.lrange("mylist", -100, 100);
System.out.println(lRangeResult4); // >>> [one, two, three]
List<String> lRangeResult5 = jedis.lrange("mylist", 5, 10);
System.out.println(lRangeResult5); // >>> []
long rPopResult1 = jedis.rpush(
"mylist", "one", "two", "three", "four", "five"
);
System.out.println(rPopResult1); // >>> 5
String rPopResult2 = jedis.rpop("mylist");
System.out.println(rPopResult2); // >>> five
List<String> rPopResult3 = jedis.rpop("mylist", 2);
System.out.println(rPopResult3); // >>> [four, three]
List<String> rPopResult4 = jedis.lrange("mylist", 0, -1);
System.out.println(rPopResult4); // >>> [one, two]
long rPushResult1 = jedis.rpush("mylist", "hello");
System.out.println(rPushResult1); // >>> 1
long rPushResult2 = jedis.rpush("mylist", "world");
System.out.println(rPushResult2); // >>> 2
List<String> rPushResult3 = jedis.lrange("mylist", 0, -1);
System.out.println(rPushResult3); // >>> [hello, world]
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
".com/redis/go-redis/v9"
)
func ExampleClient_cmd_llen() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
lPushResult1, err := rdb.LPush(ctx, "mylist", "World").Result()
if err != nil {
panic(err)
}
fmt.Println(lPushResult1) // >>> 1
lPushResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result()
if err != nil {
panic(err)
}
fmt.Println(lPushResult2) // >>> 2
lLenResult, err := rdb.LLen(ctx, "mylist").Result()
if err != nil {
panic(err)
}
fmt.Println(lLenResult) // >>> 2
}
func ExampleClient_cmd_lpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
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]
}
func ExampleClient_cmd_lpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
lPushResult1, err := rdb.LPush(ctx, "mylist", "World").Result()
if err != nil {
panic(err)
}
fmt.Println(lPushResult1) // >>> 1
lPushResult2, err := rdb.LPush(ctx, "mylist", "Hello").Result()
if err != nil {
panic(err)
}
fmt.Println(lPushResult2) // >>> 2
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [Hello World]
}
func ExampleClient_cmd_lrange() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
RPushResult, err := rdb.RPush(ctx, "mylist",
"one", "two", "three",
).Result()
if err != nil {
panic(err)
}
fmt.Println(RPushResult) // >>> 3
lRangeResult1, err := rdb.LRange(ctx, "mylist", 0, 0).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult1) // >>> [one]
lRangeResult2, err := rdb.LRange(ctx, "mylist", -3, 2).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult2) // >>> [one two three]
lRangeResult3, err := rdb.LRange(ctx, "mylist", -100, 100).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult3) // >>> [one two three]
lRangeResult4, err := rdb.LRange(ctx, "mylist", 5, 10).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult4) // >>> []
}
func ExampleClient_cmd_rpop() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
rPushResult, err := rdb.RPush(ctx, "mylist",
"one", "two", "three", "four", "five",
).Result()
if err != nil {
panic(err)
}
fmt.Println(rPushResult) // >>> 5
rPopResult, err := rdb.RPop(ctx, "mylist").Result()
if err != nil {
panic(err)
}
fmt.Println(rPopResult) // >>> five
rPopCountResult, err := rdb.RPopCount(ctx, "mylist", 2).Result()
if err != nil {
panic(err)
}
fmt.Println(rPopCountResult) // >>> [four three]
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [one two]
}
func ExampleClient_cmd_rpush() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
rPushResult1, err := rdb.RPush(ctx, "mylist", "Hello").Result()
if err != nil {
panic(err)
}
fmt.Println(rPushResult1) // >>> 1
rPushResult2, err := rdb.RPush(ctx, "mylist", "World").Result()
if err != nil {
panic(err)
}
fmt.Println(rPushResult2) // >>> 2
lRangeResult, err := rdb.LRange(ctx, "mylist", 0, -1).Result()
if err != nil {
panic(err)
}
fmt.Println(lRangeResult) // >>> [Hello World]
}
Give these commands a try in the interactive console:
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.
One of the following:
- Null 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.