![]() ![]() You see that the length of the resulting tuples will always equal the number of iterables you pass as arguments. Python zip() function can take just one argument and returns an iterator that yields a series of 1-item tuples. However, since zipped holds the empty iterator, there’s nothing to pull out, so Python raises the StopIteration exception. Python tries to retrieve the next item when you call the next() on zipped. In this case, you’ll get a StopIteration exception. You could also force the empty iterator to yield an element directly. Likewise, if you consume the iterator with a list(), you’ll see an empty list. You call zip() with no arguments, so your zipped variable holds an empty iterator. Thus, to retrieve a final list object, you need to use the list() to consume the iterator. ![]() Notice how the Python zip() function returns the iterator. The x values are taken from the numbers, and the y values are taken from letters. Here, you use the zip(numbers, letters) to create the iterator that produces tuples of the form (x, y). If you use the zip() with n arguments, that function will return the iterator that generates tuples of length n. The type() is a built-in function that returns the Python object type. To check the zip object in Python, use the type() function. It’s because the iterator stops when the shortest iterable is exhausted. Then, a returned iterator has three tuples.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |