Search code examples
pythondictionaryrandom

Get a random subset of a dictionary


I have a very large dictionary, and I want to extract a subsample, on which I then want to iterate. I tried:

import random
dictionary = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
keys = random.sample(dictionary, 3)
sample = dictionary[keys]

But it doesn't work:

Traceback (most recent call last):
  File "[..]/foobar.py", line 4, in <module>
    sample = dictionary[keys]
TypeError: unhashable type: 'list'

This works:

import random
dictionary = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
keys = random.sample(dictionary, 3)
sample = {key: dictionary[key] for key in keys}

It seems a bit word-ish: I hoped there would be a vectorized way to build the new dictionary. However, is this the right/most Pythonic way to do it? Also, if I want to iterate on this sample, should I do like this:

for key, value in sample.iteritems():
    print(key, value)

Solution

  • With

    dict(random.sample(dictionary.items(), N))
    

    you can select N random (key, value) pairs from your dictionary and pass them to the dict constructor.

    Demo:

    >>> import random
    >>> dictionary = dict(enumerate(range(10)))
    >>> dictionary
    {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
    >>> N = 3
    >>> dict(random.sample(dictionary.items(), N))
    {3: 3, 6: 6, 9: 9}