EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Software Development Software Development Tutorials Redis Tutorial Redis LPUSH

Redis LPUSH

Updated February 17, 2023

Introduction to Redis LPUSH

Redis LPUSH is used to insert the value specified in the head list and saved it in the key. If the key does not exist, it will create an empty list before performing the LPUSH operations. When a key contains a value that is not in the list, an error is returned. When we use LPUSH, we use the lrange method.

REDIS LPUSH

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Key Takeaways

  • While using redis LPUSH command, it is possible to insert single as well as multiple values in a single command.
  • The list is used by Redis LPUSH to store the value. When inserting data into the redis key, we only used the key type as a list.

What is Redis LPUSH?

The redis LPUSH command inserts the specified values that were heading in the value from the specified key. Using redis LPUSH, we can push multiple elements with a single command; we simply need to specify the arguments at the end of the command. Using redis LPUSH, elements will be inserted one by one, with the last element inserted at the top of the list, from leftmost to rightmost. If we insert x, y, and z elements, the output will have z in the first position, y in the second position, and x in the last position.

Syntax:

LPUSH name_of_key val1 …. ValN

We can insert multiple elements of values into a single command by using LPUSH. LPUSH is the command used to insert the values in the syntax above. When using the LPUSH command, we must specify the key that will be used to store the element. When we use the LPUSH command, we only use one key. Value is the parameter that was used to define the value when using the redis LPUSH command.

How to Use Redis LPUSH?

To use it we are using the ubuntu system for installing the redis server. While using the LPUSH we need to install redis server in our system.

1. First, we are installing the redis server in our system. In the below example, we are installing the redis server using the command name as apt as follows.

Code:

# apt install redis-server
# redis-server --version

Output:

Installing Redis LPUSH

2. While using redis LPUSH now in this step we are login into the redis database by using the below command as follows. While executing the LPUSH command we need to login into redis cli by using the redis-cli command as follows.

Code:

# redis-cli

Output:

Redis LPUSH - CLI command

3. Now in this step we are using the redis LPUSH method to insert the element. In the below example, we are using single key and single value to use the redis LPUSH command.

Code:

LPUSH key_lpush "lpush_val1"

Output:

Redis LPUSH Method

4. As we can see in the above example, we have used the key name as “key_lpush” and defined value as “lpush_val1”. Now in this step, we are adding a new value to the key name as key_lpush as follows.

Code:

LPUSH key_ lpush " lpush_val2"

Output:

adding new value to the key name

5. While adding the value into the key, now in this step we are using the LRANGE command to retrieve the element which we have defined. The below example shows how we can retrieve the value by using lrange which was defined in LPUSH as follows.

Code:

LRANGE key_lpush 0 -1

Output:

Redis LPUSH - Retrieve the value

Redis LPUSH Command

The Redis LPUSH command is useful for inserting values because it allows us to insert single and multiple values in the same command. The command has been available since version 1.0.0. After the push operations are completed, the command will return the integer that was responding to the list length. The LPUSH command’s return value type is an integer. Basically, the LPUSH command adds a new element to the list’s left side.

The following example shows how we can use the LPUSH command to insert a single element into the key. In the example below, we can see that we first inserted the val1 element and then the val2 element, but when we retrieve the value from the key using the lrange command, it shows the val2 element first, then the val1 element, because the last inserted element comes first when using the LPUSH command.

Code:

LPUSH redis_lpush "val1"
LPUSH redis_lpush "val2"
LRANGE redis_lpush 0 1

Output:

Redis LPUSH - Single Element

If we have already created a key with the type string, we cannot add the element by using LPUSH into the specified key. It will return the error that the key has the incorrect value. In the following example, the key contains a string value, and we add an integer value, resulting in an error.

Code:

LPUSH color 1
TYPE color

Output:

Redis LPUSH - String value

We can insert the data into the key when it holds the type as list. In the below example, the key name redis_LPUSH1 is holding the type as list, so we can be able to insert the element into the specified key.

Code:

Type redis_lpush1
LPUSH redis_lpush1 "val"
LRANGE redis_lpush1 0 1

Output:

insert the element into specified key

The below example shows how we can insert multiple elements by using the LPUSH command as follows. In the example below, we can see that it works the same as when inserting a single element. In the below example, we are inserting val1, val2, and val3 elements respectively, but we can see that it will retrieve the element as val3, val2, and val1 respectively.

Code:

LPUSH redis_lpush2 val1 val2 val3
LRANGE redis_lpush2 0 2

Output:

Redis LPUSH - Retrieve the elements

Examples of Redis LPUSH

Given below are the examples mentioned:

Example #1

In the example below, we are inserting a single element. As shown below, we define the key name as color1 and the value as white. We are also retrieving the value using the lrange method.

Code:

LPUSH color1 "white"
LRANGE color1 0 1

Output:

retrieving the value using the lrange method

Example #2

In the example below, we are inserting a single element. As shown below, we define the key name as color1 and the value as blue, red, and yellow. We are also retrieving the value using the lrange method.

Code:

LPUSH color1 "blue" "red" "yellow"
LRANGE color1 0 2
LRANGE color1 0 3

Output:

inserting a single element

Example #3

In the below example, we are adding two integer elements by using LPUSH.

Code:

LPUSH integer_key 11 13
LRANGE integer_key 0 2

Output:

adding two integer elements

Conclusion

The redis LPUSH command inserts the specified values that were heading into the value from the specified key. It is possible to push multiple elements with a single command by using it. It is used to insert the value specified in the head list and saved it in the key.

Recommended Articles

This is a guide to Redis LPUSH. Here we discuss the introduction, to how to use redis LPUSH? and examples respectively. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW