Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:28+00:00 2026-05-18T05:19:28+00:00

I have a dict that has many elements, I want to write a function

  • 0

I have a dict that has many elements, I want to write a function that can return the elements in the given index range(treat dict as array):

get_range(dict, begin, end):
    return {a new dict for all the indexes between begin and end}

How that can be done?

EDIT: I am not asking using key filter… eg)

{"a":"b", "c":"d", "e":"f"}

get_range(dict, 0, 1) returns {"a":"b", "c":"d"} (the first 2 elements)

I don’t care the sorting…
Actually I am implementing the server side paging…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:19:29+00:00Added an answer on May 18, 2026 at 5:19 am

    Edit: A dictionary is not ordered. It is impossible to make get_range return the same slice whenever you have modified the dictionary. If you need deterministic result, replace your dict with a collections.OrderedDict.

    Anyway, you could get a slice using itertools.islice:

    import itertools
    def get_range(dictionary, begin, end):
      return dict(itertools.islice(dictionary.iteritems(), begin, end+1)) 
    

    The previous answer that filters by key is kept below:

    With @Douglas‘ algorithm, we could simplify it by using a generator expression:

    def get_range(dictionary, begin, end):
      return dict((k, v) for k, v in dictionary.iteritems() if begin <= k <= end)
    

    BTW, don’t use dict as the variable name, as you can see here dict is a constructor of dictionary.

    If you are using Python 3.x, you could use dictionary comprehension directly.

    def get_range(dictionary, begin, end):
      return {k: v for k, v in dictionary.items() if begin <= k <= end}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2':
I have an versioned document store which I want to access through an dict
Many thanks for the advice you have given me thus far. Using testbenches is
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a dict, which I need to pass key/values as keyword arguments.. For
I have a productdatabase that contains products, parts and labels for each part based
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I want to have generalised email templates. Currently I have multiple email templates with
Have just started using Google Chrome , and noticed in parts of our site,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.