diff options
author | Mariusz Skoneczko <mariusz@skoneczko.com> | 2019-10-22 12:08:08 +1100 |
---|---|---|
committer | Mariusz Skoneczko <mariusz@skoneczko.com> | 2019-10-22 12:08:23 +1100 |
commit | 2486fa8c1e51e975c603fa7972542deae287817b (patch) | |
tree | cea766b9dd2f4cb2343c7360fa41f117d4828ffe | |
parent | ef1ccd2b0f9ca450395b6391b1155c981cd4ad4d (diff) |
[python3/en] Clarify difference between iterators and iterables in the last example (closes #3586)
-rw-r--r-- | python3.html.markdown | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/python3.html.markdown b/python3.html.markdown index 430927a9..61c53408 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -550,8 +550,14 @@ next(our_iterator) # => "three" # After the iterator has returned all of its data, it raises a StopIteration exception next(our_iterator) # Raises StopIteration -# You can grab all the elements of an iterator by calling list() on it. -list(filled_dict.keys()) # => Returns ["one", "two", "three"] +# We can also loop over it, in fact, "for" does this implicitly! +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 #################################################### |