5. Iterators
• Iterators were added to Python in version 2.2 to give sequence-like
objects a sequence-like interface.
• A=(10,20,30)
• B=iter(A)
• B.next() B.next() B.next()
6. List comprehensions
• Useful to create list easily
• map() is a function that applies an operation to list members
• filter() filters out list members based on a conditional expression.
lambda allows you to create one-line function objects on the fly.
• map(lambda x: x ** 2, range(6))
• Syntax:
[expr for iter_var in iterable]
Ex: [x ** 2 for x in range(6)]
[(x+1,y+1) for x in range(3) for y in range(5)]
7. Generator Expressions
• The one weakness of list comps is that all of the data have to be made
available in order to create the entire list.
• This can have negative consequences if an iterator with a large
dataset is involved.
• Generator expressions resolve this issue by combining the syntax and
flexibility of list comps with the power of generators.