1.2 KiB
1.2 KiB
id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
| id | source_exercise_id | title | section | source_path | source_repo | source_commit | student_visible_solution | has_private_solution | skip |
|---|---|---|---|---|---|---|---|---|---|
| practical-python-6.14 | 6.14 | Generator Expressions in Function Arguments | 6.4 More Generators | 06_Generators/04_More_generators.md | https://github.com/dabeaz-course/practical-python | 93dca856b41c61a0a0f85ae334116e4c125629ea | false | false | false |
Exercise 6.14: Generator Expressions in Function Arguments
Source: Practical Python Programming,
06_Generators/04_More_generators.md.
Exercise 6.14: Generator Expressions in Function Arguments
Generator expressions are sometimes placed into function arguments. It looks a little weird at first, but try this experiment:
>>> nums = [1,2,3,4,5]
>>> sum([x*x for x in nums]) # A list comprehension
55
>>> sum(x*x for x in nums) # A generator expression
55
>>>
In the above example, the second version using generators would use significantly less memory if a large list was being manipulated.
In your portfolio.py file, you performed a few calculations
involving list comprehensions. Try replacing these with
generator expressions.