1

I want to loop through my set in an order. I know sets are unordered and will have to sort it into a list and have done so by sorting the set numerically, but I now want to try to sort it in the order the numbers were first appended to the set. Is this possible to do after the set has been created or will this require me to the numbers to populate something other than a set? The order will need to keep the order the numbers were first appended to the set

PTrem2
  • 35
  • 6
  • 1
    No, it's not possible unless you augment the items you're adding to the set with extra information – c2huc2hu Nov 20 '17 at 16:26
  • 1
    Possible duplicate of [Does Python have an ordered set?](https://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set) – running.t Nov 20 '17 at 16:28
  • Are you required to use a set and not a list? – Roars Nov 20 '17 at 16:33
  • Not entirely necessary to use a set in the first place, could probably get around it by using a list but only populating if unique. Not quite a duplicate of that question, but I could potentially use a similar solution. How might I potentially add extra information to the set e.g. give each item a rank or something? – PTrem2 Nov 20 '17 at 16:38

2 Answers2

2

The easiest way, short of augmenting the elements inserted with some sort of ordering, is probably to use the keys of an ordereddict to store & retreive the elements of your set, with dummy values mapped to these keys.

from collections import OrderedDict
seq = [6, 7, 4, 3, 2, 1, 5, 0]
my_set = OrderedDict()
for elt in seq:
    my_set[elt] = True

You can now iterate or retrieve the keys in the order you inserted them. You get the same properties as a set, i/e uniqueness, fast insertion, retrieval, and contains; what you don't get are specific set operations like symmetric difference, etc...

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
1

No, a set is not ordered. There is no way of finding out in which order the elements were appended to the set. However, you could use a list for that. Every time you add an element to the set append it to the list as well and then you know the order. But beware: a set only contains each same element once whereas a list can contain the same element multiple times. I am not sure how this effects your use of this feature. You could work around it by only appending to the list if the element is not yet in the list.

s = set()
l = []

elem = 1
if elem not in l:
    l.append(elem)
s.add(elem)

print(s)
print(l)
mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
  • 1
    You could also create a list and convert it to a set when necessary using `set(l)` – Roars Nov 20 '17 at 16:37
  • 1
    But if I was to convert to a set I then lose the ordering once again if I am to loop through the set afterwards. I don't think I can use a set here at all if the order of appending items is lost, so I think a unique list will do the job – PTrem2 Nov 20 '17 at 16:45
  • Yes, list definitely seems the most appropriate. We were just suggesting the conversion to a set if it was necessary. (E.g. you could convert to a set to make use of its specific functionality without getting rid of the original list) – Roars Nov 20 '17 at 16:48