set.pop does not return the first element. As it clearly says in the help that you quoted, it returns an arbitrary element.
And the reason for this is simple: sets are inherently unordered. There is no meaningful "first" or "last" element.1
1. Since sets are stored as hash tables, it makes sense for both performance reasons and simplicity reasons that set.pop will remove the first element in the hash table, which will often be the same as the first element iterated by the set, but that's not guaranteed. And, in fact, exactly when it's true is different in CPython 3.6 - 3.7 than in 3.3 - 3.5.
stack - Why set pop return first element while list pop return last element in python - Stack Overflow
python - How to retrieve an element from a set without removing it? - Stack Overflow
Removing first element
Removing the first element in a file list
Two options that don't require copying the whole set:
for e in s:
break
# e is now an element from s
Or...
e = next(iter(s))
But in general, sets don't support indexing or slicing.
I wondered how the functions will perform for different sets, so I did a benchmark:
from random import sample
def ForLoop(s):
for e in s:
break
return e
def IterNext(s):
return next(iter(s))
def ListIndex(s):
return list(s)[0]
def PopAdd(s):
e = s.pop()
s.add(e)
return e
def RandomSample(s):
return sample(s, 1)
def SetUnpacking(s):
e, *_ = s
return e
from simple_benchmark import benchmark
b = benchmark([ForLoop, IterNext, ListIndex, PopAdd, RandomSample, SetUnpacking],
{2**i: set(range(2**i)) for i in range(1, 20)},
argument_name='set size',
function_aliases={first: 'First'})
b.plot()

This plot clearly shows that some approaches (RandomSample, SetUnpacking and ListIndex) depend on the size of the set and should be avoided in the general case (at least if performance might be important). As already shown by the other answers the fastest way is ForLoop.
However as long as one of the constant time approaches is used the performance difference will be negligible.
iteration_utilities (Disclaimer: I'm the author) contains a convenience function for this use-case: first:
>>> from iteration_utilities import first
>>> first({1,2,3,4})
1
I also included it in the benchmark above. It can compete with the other two "fast" solutions but the difference isn't much either way.
I find myself not using built-in syntax. I want to write from scratch. My reasoning is, this will strengthen my fundamentals. This is also draining, since I'm very new to programming, not just Python. I don't know if this is worth it - it could be causing more stress.
---------------
Here is a problem, with a very simple solution using the remove method. And I am complicating this.
I need your guidance:
a. Should I stop wrestling with writing code from scratch, and just get on with simple solutions. In problem below, not only am I trying with for loop, I also want to try with del. Am I being an idiot wasting time.
b. A solution to the problem below without using the remove method.
c. A site that explains algorithm behind methods such as remove. I've tried searching. Sample search terms I used: "python programs without using built-in methods" "remove method from scratch Python", "remove method algorithm"
-------------
Task: Create a function, remove_first, that takes a list as first argument, and an element in the list as second object. Removes the occurrence with the lowest index from the first argument. Pulls all following elements one position to the left. Modifies the list in place.
Solution given:
def remove_first(some_list, element):
some_list.remove(element)Code I am experimenting with:
def remove_first(list1,obj):
for i in range(len(list1)): if list1[i]==obj: print(i) print(list1[:i]+list1[i+1:])
remove_first(['apple','bat','crow','daisy','potato','apple'],'apple')
Output:
Output I get is wrong. I am trying to figure out how to fix this code.
0 ['bat', 'crow', 'daisy', 'potato', 'apple'] 5 ['apple', 'bat', 'crow', 'daisy', 'potato']
Thank you.