Practice Python

Chris Angelico rosuav at gmail.com
Wed May 10 08:18:15 EDT 2017


On Wed, May 10, 2017 at 10:11 PM, Andre Müller <gbs.deadeye at gmail.com> wrote:
> 1.) a short example for Python 3, but not exactly what they want.
>
> def square(numbers):
>     yield from sorted(n**2 for n in numbers)
>
> numberlist = [99, 4, 3, 5, 6, 7, 0]
> result = list(square(numberlist))

If you're going to use sorted(), why not simply return the list
directly? This unnecessarily iterates through the list and builds a
new one.

ChrisA



More information about the Python-list mailing list