Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: how to get a subset of dict

Tags:

python

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

like image 667
Bin Chen Avatar asked Nov 16 '10 13:11

Bin Chen


2 Answers

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}
like image 80
kennytm Avatar answered Oct 12 '22 22:10

kennytm


Straight forward implementation:

def get_range(d, begin, end):
    result = {}
    for (key,value) in d.iteritems():
        if key >= begin and key <= end:
            result[key] = value
    return result

One line:

def get_range2(d, begin, end):
    return dict([ (k,v) for (k,v) in d.iteritems() if k >= begin and k <= end ])
like image 21
Douglas Leeder Avatar answered Oct 12 '22 22:10

Douglas Leeder