The settings are disordered, so the βthirdβ element does not really mean anything. This will remove the arbitrary element.
If this is what you want to do, you can simply do:
data.pop() data.add(new_value)
If you want to remove an element from the set by value and replace it, you can do:
data.remove(value) #data.discard(value) if you don't care if the item exists. data.add(new_value)
If you want to keep the ordered data, use the list and do:
data[index] = new_value
To show that sets are not ordered:
>>> list(set(["dog", "cat", "elephant"])) ['elephant', 'dog', 'cat'] >>> list(set([1, 2, 3])) [1, 2, 3]
You can see that this is only a coincidence of the implementation of CPython, that "3" is the third element of the list made from the set [1, 2, 3]
.
Your sample code is also deeply corrupted in other ways. new_list
does not exist. In no case is the old element removed from the list, and the action of the cycle through the list is completely pointless. Obviously, none of this matters much, since the whole concept is wrong.