what are iterable objects
play

What are iterable objects ? P R AC TIC IN G C OD IN G IN TE R VIE - PowerPoint PPT Presentation

What are iterable objects ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran Definition iterable objects / Iterables - an y object that can be u sed in a for loop list t u ple


  1. List comprehension nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] for num in range(1, 6) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  2. List comprehension nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] [ for num in range(1, 6)] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  3. List comprehension nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] [(2 * num) for num in range(1, 6)] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  4. List comprehension nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] nums_new = [(2 * num) for num in range(1, 6)] print(nums_new) [2, 4, 6, 8, 10] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  5. S u mming u p List comprehension is de � ned b y: PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  6. S u mming u p List comprehension is de � ned b y: an iterable object ( e . g . list , t u ple , set ) [ for num in range(1, 6)] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  7. S u mming u p List comprehension is de � ned b y: an iterable object ( e . g . list , t u ple , set ) an operation on an element [(2 * num) for num in range(1, 6)] ( optional ) conditions PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  8. List comprehension w ith condition nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] 1 2 3 4 5 6 7 8 9 10 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  9. List comprehension w ith condition nums = [2, 4, 6, 8, 10] print(nums) [2, 4, 6, 8, 10] 1 2 3 4 5 6 7 8 9 10 → 2 4 6 8 10 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  10. Adding a condition nums_new = [num for num in range(1, 11)] print(nums_new) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  11. Adding a condition nums_new = [num for num in range(1, 11) if num % 2 == 0] print(nums_new) [2, 4, 6, 8, 10] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  12. More e x amples text = 'list COMPREHENSION is A way TO create LISTS' Task : Create a list that contains the length of each lo w ercased w ord . list , is , way , create → [4, 2, 3, 6] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  13. More e x amples text = 'list COMPREHENSION is A way TO create LISTS' Task : Create a list that contains the length of each lo w ercased w ord . list , is , way , create → [4, 2, 3, 6] output = [ for word in text.split() ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  14. More e x amples text = 'list COMPREHENSION is A way TO create LISTS' Task : Create a list that contains the length of each lo w ercased w ord . list , is , way , create → [4, 2, 3, 6] output = [ for word in text.split() if word.islower()] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  15. More e x amples text = 'list COMPREHENSION is A way TO create LISTS' Task : Create a list that contains the length of each lo w ercased w ord . list , is , way , create → [4, 2, 3, 6] output = [len(word) for word in text.split() if word.islower()] print(output) [4, 2, 3, 6] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  16. M u ltiple loops numbers = [1, 2, 3] letters = ['a', 'b', 'c'] Create all the possible pairs bet w een numbers and letters : [ (1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  17. Iterating thro u gh m u ltiple loops numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [ for i in numbers ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  18. Iterating thro u gh m u ltiple loops numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [ for i in numbers for j in letters] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  19. Iterating thro u gh m u ltiple loops numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [(i, j) for i in numbers for j in letters] print(pairs) [ (1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3, 'b'), (3, 'c'), ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  20. Deeper look numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [(i, j) for i in numbers for j in letters] pairs = [] for i in numbers: for j in letters: pairs.append((i, j)) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  21. Deeper look numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [(i, j) for j in letters] pairs = [] for j in letters: pairs.append((i, j)) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  22. Deeper look numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [(i, j) for i in numbers ] pairs = [] for i in numbers: pairs.append((i, j)) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  23. Adding sq u are brackets pairs = [ (i, j) for i in numbers for j in letters] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  24. Adding sq u are brackets pairs = [[(i, j) for i in numbers] for j in letters] print(pairs) [ [(1, 'a'), (2, 'a'), (3, 'a')], [(1, 'b'), (2, 'b'), (3, 'b')], [(1, 'c'), (2, 'c'), (3, 'c')] ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  25. Adding sq u are brackets pairs = [[(i, j) for i in numbers] for j in letters] pairs = [] for j in letters: temp = [] for i in numbers: temp.append((i, j)) pairs.append(temp) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  26. Adding sq u are brackets pairs = [[(i, j) for i in numbers] ] pairs = [] temp = [] for i in numbers: temp.append((i, j)) pairs.append(temp) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  27. Adding sq u are brackets pairs = [[(i, j) ] for j in letters] pairs = [] for j in letters: temp = [] temp.append((i, j)) pairs.append(temp) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  28. S w ap n u mbers and letters numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [[(i, j) for i in numbers] for j in letters] print(pairs) [ [(1, 'a'), (2, 'a'), (3, 'a')], [(1, 'b'), (2, 'b'), (3, 'b')], [(1, 'c'), (2, 'c'), (3, 'c')] ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  29. S w ap n u mbers and letters numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [[(i, j) for i in letters] for j in numbers] print(pairs) [ [('a', 1), ('b', 1), ('c', 3)], [('a', 2), ('b', 2), ('c', 3)], [('a', 3), ('b', 3), ('c', 3)] ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  30. Difference bet w een list comprehensions numbers = [1, 2, 3] letters = ['a', 'b', 'c'] pairs = [(i, j) for i in numbers for j in letters] pairs = [[(i, j) for i in numbers] for j in letters] pairs = [[(i, j) for i in letters] for j in numbers] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  31. Let ' s practice ! P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

  32. What is a z ip object ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran

  33. Definition zip - object that combines se v eral iterable objects into one iterable object . e - an element from an Iterable i PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  34. Definition zip - object that combines se v eral iterable objects into one iterable object . e - an element from an Iterable i PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  35. Definition zip - object that combines se v eral iterable objects into one iterable object . e - an element from an Iterable i PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  36. E x ample title = 'TMNT' villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady'] turtles = { 'Raphael': 'Sai', 'Michelangelo': 'Nunchaku', 'Leonardo': 'Twin katana', 'Donatello': 'Bo' } result = zip(title, villains, turtles) print(result) <zip object at 0x7f37bab6e608> PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  37. Tra v ersing thro u gh a z ip object result = zip(title, villains, turtles) for item in result: print(item) ('T', 'Shredder', 'Raphael') ('M', 'Krang', 'Michelangelo') ('N', 'Bebop', 'Leonardo') ('T', 'Rocksteady', 'Donatello') PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  38. Ret u rning a list of t u ples result = zip(title, villains, turtles) tuples = list(result) print(tuples) [ ('T', 'Shredder', 'Raphael'), ('M', 'Krang', 'Michelangelo'), ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello') ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  39. z ip object as Iterator result = zip(title, villains, turtles) next(result) ('N', 'Bebop', 'Leonardo') next(result) next(result) ('T', 'Shredder', 'Raphael') ('T', 'Rocksteady', 'Donatello') next(result) next(result) ('M', 'Krang', 'Michelangelo') StopIteration PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  40. z ip object is e x pendable result = zip(title, villains, turtles) for item in result: print(item) ('T', 'Shredder', 'Raphael') ('M', 'Krang', 'Michelangelo') ('N', 'Bebop', 'Leonardo') ('T', 'Rocksteady', 'Donatello') PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  41. z ip object is e x pendable result = zip(title, villains, turtles) result = zip(title, villains, turtles) for item in result: tuples = list(result) print(item) print(tuples) ('T', 'Shredder', 'Raphael') [ ('M', 'Krang', 'Michelangelo') ('T', 'Shredder', 'Raphael'), ... ('M', 'Krang', 'Michelangelo'), ('N', 'Bebop', 'Leonardo'), ('T', 'Rocksteady', 'Donatello') for item in result: ] print(item) # nothing PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  42. 'z ip ' object is e x pendable result = zip(title, villains, turtles) result = zip(title, villains, turtles) for item in result: tuples = list(result) print(item) print(tuples) ('T', 'Shredder', 'Raphael') [ ('M', 'Krang', 'Michelangelo') ('T', 'Shredder', 'Raphael'), ... ... for item in result: tuples = list(result) print(item) print(tuples) # nothing [] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  43. Uneq u al Iterable si z es title = 'TMNT' villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady'] turtles = { 'Raphael': 'Sai', 'Michelangelo': 'Nunchaku', 'Leonardo': 'Twin katana', 'Donatello': 'Bo' } PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  44. Uneq u al Iterable si z es title = 'Teenage Mutant Ninja Turtles' villains = ['Shredder', 'Krang', 'Bebop', 'Rocksteady'] turtles = { 'Raphael': 'Sai', 'Michelangelo': 'Nunchaku', 'Leonardo': 'Twin katana', 'Donatello': 'Bo' } result = zip(title, villains, turtles) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  45. Tra v ersing thro u gh the 'z ip ' object result = zip(title, villains, turtles) for item in result: print(item) ('T', 'Shredder', 'Raphael') ('e', 'Krang', 'Michelangelo') ('e', 'Bebop', 'Leonardo') ('n', 'Rocksteady', 'Donatello') PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  46. Re v erse operation turtle_masks = [ ('Raphael', 'red'), ('Michelangelo', 'orange'), ('Leonardo', 'blue'), ('Donatello', 'purple') ] result = zip(*turtle_masks) print(result) [ ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'), ('red', 'orange', 'blue', 'purple') ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  47. Uneq u al t u ple si z es turtle_masks = [ ('Raphael', 'red'), ('Michelangelo', 'orange'), ('Leonardo', 'blue', 'cyan'), ('Donatello', 'purple', 'magenta') ] result = zip(*turtle_masks) print(result) [ ('Raphael', 'Michelangelo', 'Leonardo', 'Donatello'), ('red', 'orange', 'blue', 'purple') ] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  48. Relation to a dictionar y A zip object can be u sed to create a { 'director': [ dictionar y 'R.Zemeckis', 'M.Scorsese', keys = ['movie', 'year', 'director'] 'D.Fincher' values = [ ], ['Forest Gump', 'Goodfellas', 'Se7en'], 'movie': [ [1994, 1990, 1995], 'Forest Gump', ['R.Zemeckis', 'M.Scorsese', 'D.Fincher'] 'Goodfellas', ] 'Se7en' ], movies = dict(zip(keys, values)) 'year': [1994, 1990, 1995] } print(movies) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  49. Creating a DataFrame import pandas as pd df_movies = pd.DataFrame(movies) print(df_movies) director movie year 0 Robert Zemeckis Forest Gump 1994 1 Martin Scorsese Goodfellas 1990 2 David Fincher Se7en 1995 list() PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  50. Creating a DataFrame import pandas as pd df_movies = pd.DataFrame(movies) print(df_movies) director movie year 0 Robert Zemeckis Forest Gump 1994 1 Martin Scorsese Goodfellas 1990 2 David Fincher Se7en 1995 list() → zip() PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  51. Creating a DataFrame import pandas as pd df_movies = pd.DataFrame(movies) print(df_movies) director movie year 0 Robert Zemeckis Forest Gump 1994 1 Martin Scorsese Goodfellas 1990 2 David Fincher Se7en 1995 list() → zip() → dict() PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  52. Creating a DataFrame import pandas as pd df_movies = pd.DataFrame(movies) print(df_movies) director movie year 0 Robert Zemeckis Forest Gump 1994 1 Martin Scorsese Goodfellas 1990 2 David Fincher Se7en 1995 list() → zip() → dict() → DataFrame() PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  53. Let ' s practice ! P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON

  54. What is a generator and ho w to create one ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran

  55. Definition Generator - a special iterable object created b y a f u nction ha v ing a yield ke yw ord in its bod y. def func(): # Return a value from super complex calculations return 0 result = func() print(result) 0 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  56. Definition Generator - a special iterable object created b y a f u nction ha v ing a yield ke yw ord in its bod y. def func(): # Yield a value from super complex calculations yield 0 result = func() print(result) <generator object result at 0x105736e10> PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  57. Generator as Iterable def func(): # Yield a value from super complex calculations yield 0 result = func() for item in result: print(item) 0 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  58. More y ields ! def func(): yield 0 yield 1 yield 2 result = func() for item in result: print(item) 0 1 2 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  59. Yield in a loop def func(n): for i in range(0, n): yield 2*i result = func(3) for item in result: print(item) 0 2 4 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  60. Con v erting a generator to a list def func(n): for i in range(0, n): yield 2*i result = func(5) list(result) [0, 2, 4, 6, 8] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  61. Generator as Iterator next(result) Generator is an Iterable AND an Iterator def func(n): 2 for i in range(0, n): yield 2*i next(result) result = func(3) 4 next(result) next(result) 0 StopIteration PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  62. Generators are e x pendable def func(n): for item in result: for i in range(0, n): print(item) yield 2*i # nothing result = func(3) result = func(3) for item in result: for item in result: print(item) print(item) 0 0 2 2 4 4 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  63. Generators are e x pendable def func(n): list(result) for i in range(0, n): yield 2*i [] result = func(3) result = func(3) list(result) list(result) [0, 2, 4] [0, 2, 4] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  64. Generator comprehension result = [2*i for i in range(0, 3)] print(result) [0, 2, 4] result = (2*i for i in range(0, 3)) print(result) <generator object result at 0x105736e10> PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  65. Tra v ersal result = (2*i for i in range(0, 3)) for item in result: print(item) 0 1 2 next(result) StopIteration PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  66. Wh y generators ? [1, 3, 2, 4, 3, 5] simple w a y to create a c u stom iterable object def create_jump_sequence(n): for i in range(1, n-1): yield i yield i+2 jump_sequence = create_jump_sequence(4) list(jump_sequence) [1, 3, 2, 4, 3, 5] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  67. Wh y generators ? [1, 3, 2, 4, 3, 5, 4, 6, 5, 7, ...] simple w a y to create a c u stom iterable object def create_jump_sequence(n): for i in range(1, n-1): la zy initiali z ation yield i yield i+2 jump_sequence = create_jump_sequence(500) next(jump_sequence) 1 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

  68. Wh y generators ? def create_inf_generator(): simple w a y to create a c u stom iterable while True: object yield 'I am infinite!' la zy initiali z ation inf_generator = create_inf_generator() possibilit y to create in � nite iterable objects next(inf_generator) I am infinite PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON

Recommend


More recommend