views:

89

answers:

5

Is there any idiom for getting an arbitrary key, value pair from a dictionary without removing them? (P3K)

EDIT:

Sorry for the confusing wording.

I used the word arbitrary in the sense that I don't care about what I'm getting.

It's different from random, where I do care about what I'm getting (i.e., I need probabilities of each item being chosen to be the same).

And I don't have a key to use; if I did, I'd think it would be in the RTFM category and wouldn't deserve an answer on SO.

EDIT:

Unfortunately in P3K, .items() returns a dict_items object, unlike Python 2 which returned an iterator:

ActivePython 3.1.2.4 (ActiveState Software Inc.) based on
Python 3.1.2 (r312:79147, Sep 14 2010, 22:00:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d = {1:2}
>>> k,v = next(d.items())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: dict_items object is not an iterator
+3  A: 
k, v = next(d.items())
eumiro
2.x users (eg. just about everyone except the OP), be sure to use `iteritems`.
Glenn Maynard
@Glenn - exactly. First I wrote it with `iteritems()`, then I saw this P3K and had to think about what's new in P3K.
eumiro
Thanks... I started learning Python, and assumed I should choose the latest version.. :)
max
Oh man, it doesn't work :( see my edit to the question. So I suppose I have to use `k, v = next(iter(d.items()))`
max
+1  A: 
import random
k, v = random.choice(d.items())
AndrewBC
"Arbitrary" does not mean "random"; there's no reason for any randomization here.
Glenn Maynard
@Glenn Maynard. How do you know that? Getting an "arbitrary" item is what `get()` or `[]` does. If that trivial, obvious use of `get()` isn't the right answer, then there must be some mysterious other forces at work. Like randomness.
S.Lott
`get` and `[]` retrieve a *specific* item, not an arbitrary one, and are irrelevant because you have to know a key in advance to use them. (Is he really asking me how I know what the word "arbitrary" means?)
Glenn Maynard
@Glenn Maynard. OP probably did not mean choosing randomly, but it's simply the difference between getting an arbitrary something, and getting a something arbitrarily. It could be a valid inference to make, but it probably is not in this case.
AndrewBC
A: 

pop is supposed to remove a item. Isn't that the meaning of that method?

If you want to get random key , value pair use pseudo random module

>>> import random
>>> x = {1:3, 4:5, 6:7}
>>> x = {1:3, 4:5, 6:7, 'a':9, 'z':'x'}
>>> k = random.choice(x.keys())
>>> x[k]
7
>>> 
pyfunc
See @Glenn Maynard's comment on AndrewBC's answer.
martineau
A: 

The items() method returns the copy of key, value pairs of dictionary as list. For example, >fruits_color = {'apple': 'red', 'banana': 'green'}
>fruits_color.items()
[('apple', 'red'), ('banana', 'green')]

Once you have the list you can pick the key-value pairs.

kuriouscoder
A: 

Get a "arbitrary" key-vaue pair?

k, v = k, d[k]
S.Lott