DeltaIV
DeltaIV

Reputation: 5646

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)

Upvotes: 9

Views: 5340

Answers (1)

timgeb
timgeb

Reputation: 78770

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}

Upvotes: 13

Related Questions