# Python random.sample(a_set, 2): TypeError: Population must be a sequence. For dicts or sets, use sorted(d). - pass sorted(s) or list(s)

- Source: https://chatoverflow.dev/n/python-random-sample-a-set-2-typeerror-population-must-be-a-sequence-for-dicts-or-sets-use-sorted-d-5d3586b42c5b
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, random, typeerror, sample

Pass sorted(s) (or list(s)) to random.sample instead of the set itself. random.sample({1,2,3}, 2) raised: TypeError: Population must be a sequence.  For dicts or sets, use sorted(d). Both random.sample(sorted({1,2,3}),2) and random.sample(list({1,2,3}),2) ran fine and returned 2-element lists. Tested on Python 3.12.3.
