EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • 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 LRANGE
 

Redis LRANGE

Definition of Redis LRANGE

Redis LRANGE is a command that returns elements or values stored at the key in the specified range of the lists. It allows you to specify a start and stop position for the elements to return. The start and stop offsets are zero-based indexes; hence the first and second elements of the list are 0 and 1, respectively, and so on. LRANGE also allows negative indexes which retrieve elements from the tail of the lists, where the last and second last elements are -1 and -2, respectively, and so on.

Redis LRANGE

 

 

Key Highlights

  • LRANGE is a command used in Redis Lists.
  • LRANGE displays a specific list of elements stored at the key based on the range specified.
  • The offsets start and stop are zero-based indexes; therefore, the first element is always represented as 0.
  • LRANGE allows negative indexing where -1 denotes the last element and -2 denotes the second last element of the list, and so on.

How to Use Redis LRANGE?

The below syntax supports Redis LRANGE.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

  • The KEY_NAME is the name of the specific key where the elements are stored.
  • The START denotes the first element you want to return.
  • The STOP denotes the last element you want to return.

Before we start moving to the core, please install Redis in your terminal for better understanding and practicing purposes.

Redis LRANGE Command Explained

We know that elements in the lists are stored in an ordered sequence.

Example:

LRANGE command helps to retrieve the elements of the list by specifying the start and stop range without affecting the sequence. The LRANGE command displays elements based on the specified range and also supports negative indexing of the elements in the lists.

a) Displaying a Specific Set of Elements
  • First, we created the lists by adding various elements to the key eduCBA.
  • The RPUSH command adds elements to the right of the list.
  • The LRANGE command with the specified range of 0 to 2 returns the elements.
  • Where redis is 0, the tutorial is 1, and the guide is 2.

Redis Tutorial

b) Displaying all the Elements
  • In this example, the command range of 0 to -1 denotes the first element (redis) and the last element (students), respectively.
  • Hence all the elements from 0 to -1 are returned.

All Elements

c) Using Negative Indexes
  • Here the range 0 denotes the first element (redis), and -2 denotes the second last element (guide) of the list.
  • Hence the command returns all the elements from 0 to -2 of the list.

Redis LRANGE - Command

d) Using out-of-range indexes

1. STOP > size of the list

In the below example, the stop index is 8, which is greater than the size of the list; hence it is considered the last element, and the command returns the elements of the list.

Redis LRANGE - Elements of the List

2. START > size of the list

In the below example, the start position is 5, which is greater than the size of the list; hence the command returns an empty list.

Redis LRANGE - Empty List

Conclusion

Since the Redis LRANGE command is used in List to retrieve data, It finds its application in retrieving desired data from a database consisting of ample data.

FAQs

Q1. What is Redis LRANGE?

Answer: LRANGE is a Redis list command that retrieves the list of elements based on the start and stop offsets. Both the start and stop offsets are zero-based indexes where 0 represents the first element, and -1 represents the last element.

Q2. How LRANGE handles out-of-range offsets?

Answer: The out-of-range offsets are handled in two ways. If the start offset is greater than the size of the list, then the command returns an empty list. In contrast, If the stop offset is greater than the size of the list, then the last element is considered the stop offset, and the command returns the elements.

Q3. Does Redis LRANGE allow Negative indexing?

Answer: Yes, LRANGE supports negative indexing and displays data from the tail of the list. Where -1 represents the last element and -2 represents the second last element, and so on.

Recommended Articles

This article explains everything about Redis LRANGE. To know more about related topics, visit the following links:

  1. Redis Operator
  2. Redis HMGET
  3. Redis INFO
  4. Redis Memory Usage

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
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

© 2025 - 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
EDUCBA

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

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 Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW