{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#zip\n", "\n", "zip() makes an iterator that aggregates elements from each of the iterables.\n", "\n", "Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. \n", "\n", "zip() is equivalent to:\n", "\n", " def zip(*iterables):\n", " # zip('ABCD', 'xy') --> Ax By\n", " sentinel = object()\n", " iterators = [iter(it) for it in iterables]\n", " while iterators:\n", " result = []\n", " for it in iterators:\n", " elem = next(it, sentinel)\n", " if elem is sentinel:\n", " return\n", " result.append(elem)\n", " yield tuple(result)\n", " \n", "\n", "zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. \n", "\n", "Lets see it in action in some examples:\n", "\n", "##Examples" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(1, 4), (2, 5), (3, 6)]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3]\n", "y = [4,5,6]\n", "\n", "# Zip the lists together\n", "zip(x,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how tuples are returned. What if one iterable is longer than the other?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(1, 4), (2, 5), (3, 6)]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [1,2,3]\n", "y = [4,5,6,7,8]\n", "\n", "# Zip the lists together\n", "zip(x,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how the zip is defined by the shortest iterable length. Its generally advised not to zip unequal length iterables unless your very sure you only need partial tuple pairings.\n", "\n", "What happens if we try to zip together dictionaries?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('a', 'c'), ('b', 'd')]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1 = {'a':1,'b':2}\n", "d2 = {'c':4,'d':5}\n", "\n", "zip(d1,d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This makes sense because simply iterating through the dictionaries will result in just the keys. We would have to call methods to mix keys and values:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('c', 1), ('d', 2)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zip(d2,d1.itervalues())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! Finally lets use zip a to switch the keys and values of the two dictionaries:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def switcharoo(d1,d2):\n", " dout = {}\n", " \n", " for d1key,d2val in zip(d1,d2.itervalues()):\n", " dout[d1key] = d2val\n", " \n", " return dout" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'a': 4, 'b': 5}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "switcharoo(d1,d2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! You can use zip to save a lot of typing in many situations! You should now have a good understanding of zip() and some possible use cases." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }