To see this, consider the following problem: There are two main reasons why such an “iterator algebra” is useful: improved memory efficiency (via lazy evaluation) and faster execuction time. So, in a way, if you have ever used zip() or map() in Python 3, you have already been using itertools! In Python 3, izip() and imap() have been removed from itertools and replaced the zip() and map() built-ins. To return an iterator, the izip() and imap() functions of itertools must be used. Historical Note: In Python 2, the built-in zip() and map() functions do not return an iterator, but rather a list. This is what is meant by the functions in itertools forming an “iterator algebra.” itertools is best viewed as a collection of building blocks that can be combined to form specialized “data pipelines” like the one in the example above. Multiple assignment and tuple unpacking improve Python code readability.Chapter 6 of Python Tricks: The Book by Dan Bader.Python Iterators: A Step-By-Step Introduction.If you aren’t, or if you need to brush up on your knowledge, consider checking out the following before reading on: Before diving in, you should be confident using iterators and generators in Python 3, multiple assignment, and tuple unpacking. Rather than introducing itertools to you one function at a time, you will construct practical examples designed to encourage you to “think iteratively.” In general, the examples will start simple and gradually increase in complexity.Ī word of warning: this article is long and intended for the intermediate-to-advanced Python programmer. The real power lies in composing these functions to create fast, memory-efficient, and good-looking code. The thing about itertools, though, is that it is not enough to just know the definitions of the functions it contains. The docs themselves are a great place to start. Say you have a list of tuples and want to separate the elements of each tuple into independent sequences.It has been called a “gem” and “pretty much the coolest thing ever,” and if you have not heard of it, then you are missing out on one of the greatest corners of the Python 3 standard library: itertools.Ī handful of excellent resources exist for learning what functions are available in the itertools module. Do you recall that the Python zip() function works just like a real zipper? The examples so far have shown you how Python zips things closed. The reason why there’s no unzip() function in Python is because the opposite of zip() is… well, zip(). There’s a question that comes up frequently in forums for new Pythonistas: “If there’s a zip() function, then why is there no unzip() function that does the opposite?” If you forget this detail, the final result of your program may not be quite what you want or expect. However, you’ll need to consider that, unlike dictionaries in Python 3.6, sets don’t keep their elements in order. You can also use Python’s zip() function to iterate through sets in parallel. Notice that, in the above example, the left-to-right evaluation order is guaranteed. Note: If you want to dive deeper into dictionary iteration, check out How to Iterate Through a Dictionary in Python. It produces the same effect as zip() in Python 3: This function creates an iterator that aggregates elements from each of the iterables. In these situations, consider using itertools.izip(*iterables) instead. If you regularly use Python 2, then note that using zip() with long input iterables can unintentionally consume a lot of memory. This will run through the iterator and return a list of tuples. In Python 3, you can also emulate the Python 2 behavior of zip() by wrapping the returned iterator in a call to list(). The first iteration is truncated at C, and the second one results in a StopIteration exception. Here, your call to zip() returns an iterator. # Python 3 > zipped = zip ( range ( 3 ), 'ABCD' ) > zipped # Hold an iterator > type ( zipped ) > list ( zipped ) > zipped = zip () # Create an empty iterator > zipped > next ( zipped ) Traceback (most recent call last):įile "", line 1, in next ( zipped ) StopIteration
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |