5 ms·
Python does have built-in support for getting an arbitrary key: popitem() on a dict. It removes the item from the dict, but you can always add it back. http://
by anuraggoel 18y ago
Python does have built-in support for getting an arbitrary key: popitem() on a dict. It removes the item from the dict, but you can always add it back.
http://docs.python.org/library/stdtypes.html#dict.popitem http://docs.python.org/library/stdtypes.html#dict.popitem
Edit: This method returns arbitrary (not random) results; each element is not equally likely to be picked.
- ivank 18y agoThat's "arbitrary", and not random at all. Some basic testing shows that the same dictionary keys will always lead to the same popitem order.
- antirez 18y agoI bet this scan the table from the first to the last bucket.
- anuraggoel 18y agoCorrect, nice catch.
- deleted 18y ago[deleted]