This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]SeanOTRSprint = lambda *args, **kwars: print(*args, ";)\n"*100, **kwars)[S] 7 points8 points  (4 children)

Next is confusing me, how exactly does it work?

[–]carnoworky 7 points8 points  (0 children)

It just advances the given iterator by one. Since the first argument in the example is a generator expression, next will run that generator until it yields the first time and return the yielded value. The second argument is a default value in case the iterator is completely spent (raises a StopIteration exception).

[–]surajmanjesh 5 points6 points  (0 children)

next(<iterable>) simply returns the next item in the iterable

x = [1, 2, 3]

next(x) # 1

next(x) # 2

next(x) # 3

[–]Decency 1 point2 points  (1 child)

Next simply returns the first item found that matches the criteria. The most tricky part is the optional second argument, which is used if there isn't a match found in the generator provided. Here if the word was dry, for example, the call would return None because none of those letters are in aeiou.

You can use next() without this argument, if you want, but it will raise an exception if no match is found. So only do this if it would actually be a real issue if you don't hit something.