Loading topic
for loop in Python works the same way. The loop asks an object for a stream of values and takes them one at a time until the stream ends. Once you know how that request works, enumerate and zip stop looking like special syntax. They are ordinary functions that take iterables, and their r7 min readfor loop over a list, you know that a tuple is an ordered, fixed-size group of values, and you have used dictionaries. It does not assume you have used enumerate or zip yet. The point of this chapter is the loop header itself, the part between for and `in7 min readfor i in range(len(seq)). The other is a hand-maintained counter variable. Both work in simple cases. As the code grows, they produce bugs and noise that enumerate and zip avoid. The following chapters show tho9 min readenumerate works, what it returns, where it fits, and which mistakes come up most often. It assumes you can already do the following:7 min readstart argument of enumerate. You should already be comfortable with for loops over lists, tuple unpacking in a loop header (for a, b in pairs), and the basic form for index, value in enumerate(seq). If any of those are shaky, review the earlier chapters on them firs7 min readzip when you give it equal-length inputs, which is the normal case. It assumes you can write a for loop over a list and unpack a tuple in a loop header, such as for key, value in pairs. It also assumes you know the difference between an iterable and an iterator, and you can7 min readfor loops over lists, unpack tuples in a loop header, and use zip on two sequences of equal length. It also assumes you know that zip returns an iterator. You will learn what happens when the inputs to zip have different lengths, and three ways to handle it8 min readenumerate for index-and-value loops and zip for parallel loops. This chapter combines them. You will see the standard pattern, the unpacking rules behind it, the alternative nesting orders, and what happens with unequal lengths and one-shot iterators.7 min readzip, use tuple unpacking in a for header, and know that zip stops at the shortest input. You also need to be comfortable reading dictionaries and tuples, and to know that zip returns a one-shot iterator. Earlier chapters cover6 min readenumerate or zip result more than once, and the related bugs that come from the same cause. It assumes you can already write for loops over lists, unpack tuples in a loop header, and use enumerate and zip in simple cases. It also assumes you9 min read