diff options
author | Adam Bard <github@adambard.com> | 2019-10-30 12:39:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-30 12:39:35 -0700 |
commit | 0f4725a7a4fcb60311cb6bbd1f0011c13e81b818 (patch) | |
tree | c1912cbfad39fd2bad21a1036463492cfe9c5385 | |
parent | 767329cf74695ebdd7db02b75ca4a755e55cd0ff (diff) | |
parent | 2486fa8c1e51e975c603fa7972542deae287817b (diff) |
Merge pull request #3715 from mariuszskon/iterators
[python3/en] Clarify difference between iterators and iterables in th…
-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 #################################################### |