-1
roads = set()
connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
for u, v in connections:
     roads.add((u, v))
     print(roads)
result:
{(0, 1)}
{(0, 1), (1, 3)}
{(0, 1), (2, 3), (1, 3)}
{(0, 1), (4, 0), (2, 3), (1, 3)}
{(0, 1), (4, 0), (2, 3), (4, 5), (1, 3)}

is the order it gets added to the set random? at first i thought it was placing them into the roads set at the index of 1, but then in the last print statement, it adds to the set at index 3.

DrNogNog
  • 21
  • 3
  • 2
    Sets have no order or duplicates. The order they are being printed in is arbitrary. You may just use a `list` which has duplicates and orders – Freddy Mcloughlan Aug 07 '22 at 23:46
  • 3
    Does this answer your question? ['order' of unordered Python sets](https://stackoverflow.com/questions/12165200/order-of-unordered-python-sets) – Freddy Mcloughlan Aug 07 '22 at 23:47

1 Answers1

3

A Python set is an unordered collection.

So set items don't have an index. And the order in which they are printed is not defined.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94