mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-15 11:14:24 +02:00
Merge pull request #3715 from mariuszskon/iterators
[python3/en] Clarify difference between iterators and iterables in th…
This commit is contained in:
@@ -550,8 +550,14 @@ next(our_iterator) # => "three"
|
|||||||
# After the iterator has returned all of its data, it raises a StopIteration exception
|
# After the iterator has returned all of its data, it raises a StopIteration exception
|
||||||
next(our_iterator) # Raises StopIteration
|
next(our_iterator) # Raises StopIteration
|
||||||
|
|
||||||
# You can grab all the elements of an iterator by calling list() on it.
|
# We can also loop over it, in fact, "for" does this implicitly!
|
||||||
list(filled_dict.keys()) # => Returns ["one", "two", "three"]
|
our_iterator = iter(our_iterable)
|
||||||
|
for i in our_iterator:
|
||||||
|
print(i) # Prints one, two, three
|
||||||
|
|
||||||
|
# You can grab all the elements of an iterable or iterator by calling list() on it.
|
||||||
|
list(our_iterable) # => Returns ["one", "two", "three"]
|
||||||
|
list(our_iterator) # => Returns [] because state is saved
|
||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
|
Reference in New Issue
Block a user