0

When I have a set of numbers, I want to convert the set to a list or array, no matter what I use, the order of the number changes

x = {4, 7, 10, 11, 12, 17}
y = list(x)
y

and y becomes [17, 4, 7, 10, 11, 12]

  • Sets are inherently unordered - if converted to a list, the order is arbitrary. If you want order, don't use a set in the first place. – Green Cloak Guy Apr 08 '22 at 03:11
  • Python sets are not ordered. You can find some ordered set modules at pypi.org. Or, you can use dicts with a boring value such as `None` for values. dict is ordered. – tdelaney Apr 08 '22 at 03:15

0 Answers0