From the course: Python: Design Patterns
Unlock this course with a free trial
Join today to access over 24,800 courses taught by industry experts.
Iterator example - Python Tutorial
From the course: Python: Design Patterns
Iterator example
- [Instructor] Now, let's define our iterator. We start with creating a list. The list contains German words counting up to five. Here, we'll use Python's built-in iterator. The iterator will create tuples consisting of pairs of numbers and equivalent German words, for example, one and the German word eins. The tuple here means a pair. One and eins, that's a tuple, two and zwei, that's another tuple. The built-in iterator provided by Python is zip. We have to generate numbers up to five, so we use the function range here. So type range. The argument of the function is the number of German words to display, which is count, so type count. This is the first argument of the zip iterator. The second argument is the list itself. The numbers in German is our list. Type numbers_in_german. The range function here is important because it allows us to know up to what number we can count. We use a variable called iterator to store a zip instance. Now, using this iterator, we'll iterate through a…
Contents
-
-
-
-
-
-
(Locked)
Observer1m 5s
-
(Locked)
Observer example6m 22s
-
(Locked)
Solution: Observer2m 4s
-
(Locked)
Visitor56s
-
(Locked)
Visitor Example6m 42s
-
(Locked)
Iterator1m 15s
-
(Locked)
Iterator example4m 32s
-
(Locked)
Strategy40s
-
(Locked)
Strategy Example5m 44s
-
(Locked)
Solution: Strategy4m 16s
-
(Locked)
Chain of Responsibility40s
-
(Locked)
Chain of Responsibility example5m 57s
-
(Locked)
-
-