name: inverse layout: true class: center, middle, inverse --- # What are iterables? # How do I use them? PyNash October 2015 Brian Dailey [@byeliad](http://twitter.com/byeliad) --- template: inverse # What's an iterable? An iterable is anything over which you can _iterate_. If you can loop over it, it's probably an iterable. --- layout: false .left-column[ ## Example ] .right-column[ ```terminal In [1]: iterable_list = [1,2,3,4] In [2]: for i in iterable_list: ...: print(i) ...: 1 2 3 4 ``` ] --- layout: false .left-column[ ## Example ] .right-column[ ```python iterable_list = [1,2,3,4] for i in iterable_list: print(i) ``` ### Output ```terminal 1 2 3 4 ``` ] --- class: center, middle, inverse ## How does it iterate? # `object.__iter__` --- `l.__iter__()` returns an `iterator` ```terminal In [1]: l = [1,2,3,4] In [5]: pointer = l.__iter__() In [6]: pointer Out[6]: <list_iterator at 0x1116642e8> In [8]: next(pointer) Out[8]: 1 In [9]: next(pointer) Out[9]: 2 In [10]: next(pointer) Out[10]: 3 In [11]: next(pointer) Out[11]: 4 ``` An iterator has a `__next__` method. --- class: center, middle, inverse ## What happens if I call # `next(pointer)` ## on this iterator again? --- ```terminal In [11]: next(pointer) Out[11]: 4 In [12]: next(pointer) --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-12-ebf92218e69a> in <module>() ----> 1 next(pointer) StopIteration: ``` --- class: center, middle, inverse ## `StopIteration` This is how a loop knows to stop! --- This is an iterable, too! ```python s = 'cookies' ``` It has a `__getitem__` method that allows us to lookup by index. ```terminal In [13]: s = 'cookies' In [14]: s[0] Out[14]: 'c' In [15]: s[-1] Out[15]: 's' ``` --- We can also loop over the string. ```terminal In [16]: for i in s: ....: print(i) ....: c o o k i e s ``` --- Or, I can get clever and return an iterator. ```terminal In [22]: i = iter(s) In [23]: next(i) Out[23]: 'c' In [24]: next(i) Out[24]: 'o' In [25]: i.__next__() Out[25]: 'o' In [26]: i.__next__() Out[26]: 'k' ``` --- You can also cast this to a list or a tuple. ```terminal In [32]: s = 'cookies' In [33]: list(s) Out[33]: ['c', 'o', 'o', 'k', 'i', 'e', 's'] ``` --- What do you think will happen? ```terminal In [34]: s = 'cookies' In [35]: i = iter(s) In [36]: next(i) Out[36]: 'c' In [37]: next(i) Out[37]: 'o' In [38]: list(i) ``` -- ```terminal Out[38]: ['o', 'k', 'i', 'e', 's'] ``` --- ## Remember we said that an _iterable_ is an object with `__getitem__` or `__iter__` --- ## Whereas an _iterator_ is something that has a `__next__` method. --- class: middle, center, inverse # So what can we do with iterables? --- class: middle, center, inverse # Built-in methods! --- ```terminal In [4]: l = [True, True, False] In [5]: any(l) Out[5]: True In [6]: all(l) Out[6]: False In [11]: l = [('flavor', 'chocolate chip'), ('count', 'too many')] In [12]: dict(l) Out[12]: {'count': 'too many', 'flavor': 'chocolate chip'} ``` --- ```terminal In [14]: for index, detail in enumerate(l): print(index, detail) ....: 0 ('flavor', 'chocolate chip') 1 ('count', 'too many') ``` --- ```terminal In [32]: l1 = [1,2,3,4] In [33]: l2 = [5,6,7,8] In [34]: l3 = zip(l1, l2) In [35]: print(l3) ``` What will happen? -- ```terminal In [35]: print(l3) <zip object at 0x10e69b848> ``` Python3 thing here, `zip` returns an iterator! -- We can, however, cast to a list. ```terminal In [36]: print(list(l3)) [(1, 5), (2, 6), (3, 7), (4, 8)] ``` --- class: middle, center, inverse # List Comprehension! --- ```terminal In [15]: l = [1,2,3,4] In [16]: [i + 1 for i in l] Out[16]: [2, 3, 4, 5] ``` -- ```terminal In [18]: [i for i in l if i > 2] Out[18]: [3, 4] ``` --- class: middle, center, inverse # Generators! --- class: middle, center ### `raise OutOfScopeError` --- # `itertools`! --- ```terminal In [37]: import itertools In [38]: itertools.chain(l1, l2) Out[38]: <itertools.chain at 0x10e6b9470> In [39]: list(itertools.chain(l1, l2)) Out[39]: [1, 2, 3, 4, 5, 6, 7, 8] ``` ... and more! --- class: middle, center, inverse # Bonus Questions! --- class: middle, center, inverse # How do you learn? --- class: middle, center, inverse # How do you avoid burnout? --- class: middle, center, inverse ## Try not to always look at glowing rectangles. --- class: middle, center, inverse ## Have hobbies not involving computers. --- class: image, inverse  --- template: inverse # Thank you! @byeliad