-2

Is there a way of ordering a set in base python?

For example:

A = {1, 2, 3}
B = {3, -6, 2, 0}
print union(A, B)

Expected Output:

({-6, 0, 1, 2, 3}, 5)

My attempt:

x = A | B
y = len(x)
print((set(x), y))

My output:

({0, 1, 2, 3, -6}, 5)

I have read some of the answers for other questions and there are ways of doing it with various packages, but for this exercise, I am NOT meant to import any packages, just doing it in base python (if that is what it is called) if possible.

william3031
  • 1,653
  • 1
  • 18
  • 39

1 Answers1

1

No, sets are unordered by definition and implementation. To create a sorted list, convert your set to a list and sort it:

List = sorted(your_set)
ForceBru
  • 43,482
  • 10
  • 63
  • 98