Skip to content

Instantly share code, notes, and snippets.

@thecraftman
Last active July 4, 2021 21:19
Show Gist options
  • Save thecraftman/140e7a7a49911b639d920715ce32aa58 to your computer and use it in GitHub Desktop.
Save thecraftman/140e7a7a49911b639d920715ce32aa58 to your computer and use it in GitHub Desktop.
Python Generators - why to use them and the benefits you receive
def square_numbers(nums):
result = []
for i in nums:
result.append(i*i)
return result
my_nums = square_numbers([1,2,3,4,5])
# my_nums = [x * x for x in [1,2,3,4,5]]
print my_nums # [1, 4 , 9 , 16 , 25]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment