Removing items randomly from a dictionary

17,571

Solution 1

Is this what you're talking about?

import random
for i in range(4):
    some_dict.pop( random.choice(some_dict.keys()) )   

Solution 2

Removing an "arbitrary" element means that the function can remove whatever item it likes. That doesn't mean that it has to be especially random about it.

For real randomness you should use the random module. For example:

import random
for key in random.sample(d.keys(), 4):
   del d[key]

Solution 3

popitem() is arbitrary but not random. If you want to access a random element

import random
key = random.choice(d.keys())
val = d[key]
del d[key]

Solution 4

  • Why isn't popitem() working the way it should? Is removing arbitrary items not random?

Arbitrary does not mean the same thing as random. Arbitrary simply means that any of the items can be returned and nothing should be assumed about the order they are returned in.

  • How do I remove random items from a dictionary?

I do not claim this is the best or most efficient way, but one way is:

import random
dict.pop(random.choice(dict.keys())

Solution 5

For removing a specified number of items, I would use random.sample instead of making repeated calls to dict.keys and random.choice

for key in random.sample(d.keys(), n):
    del d[key] # or d.pop(key)
Share:
17,571

Related videos on Youtube

user225312
Author by

user225312

Updated on May 17, 2022

Comments

  • user225312
    user225312 about 2 years

    How do I remove random items from a dictionary in Python?

    I have to remove a specified number of items from a dictionary and so I tried to use dict.popitem which I thought was random, but it is seems it is not.

    As the docs say:

    Remove and return an arbitrary (key, value) pair from the dictionary.

    For my problem, suppose I have a dictionary like (an example):

    >>> d = dict(zip((string.ascii_lowercase), range(1, 10)))
    >>> d
    {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8}
    

    Now I need to remove some items from it (the count is specified by the user).

    So I wrote this:

    >>> for _ in range(4):          # assume 4 items have to removed
    ...     d.popitem()
    ... 
    ('a', 1)
    ('c', 3)
    ('b', 2)
    ('e', 5)
    

    But the problem with this code is, every time the script is run, popitem() pops exactly the same items. You are free to test this, I have already tried it multiple times.

    So my question is:

    • Why isn't popitem() working the way it should? Is removing arbitrary items not random?
    • How do I remove random items from a dictionary?
    • The Communist Duck
      The Communist Duck over 13 years
      Arbitrary != random.
    • user225312
      user225312 over 13 years
      Then what is this arbitrary based on?
    • Admin
      Admin over 13 years
      Propably on the internal ordering of the pairs (which is also reflected by the iteration order - which, again, is abritary but not random). Dicts are "unordered", but the order in which the items are stored internally is deterministic - if you take parameters into account you normally don't even think of: The hashes of keys, insertion order, collisions, number of total items... So creating the same dictionary in the same way again does indeed create the same internal ordering.
    • gary
      gary over 13 years
      It must be arbitrary because a dictionary itself is organized arbitrarily.
  • PascalVKooten
    PascalVKooten almost 9 years
    popitem was what I was looking for!