I defined a simple function to return the first item in an iterable, or an error:
def first(iterable):
iterator = iter(iterable)
try:
return next(iterator)
except StopIteration:
raise ValueError("iterable is empty")
In the shell, this function does what you'd expect:
>>> first({"1st", "2nd", "3rd"})
'1st'
>>> first({"1st", "2nd", "3rd"})
'1st'
>>> first(["1st", "2nd", "3rd"])
'1st'
However, in Jupyter Notebook, it returns the second element in a set
, but the first element in a list
.
first({"1st", "2nd", "3rd"})
'2nd'
first(["1st", "2nd", "3rd"])
'1st'
first(set(["1st", "2nd", "3rd"]))
'2nd'
I'm using Python 3.6.5 in Anaconda in both cases.
Am I mising something obvious?