Skip to content

Instantly share code, notes, and snippets.

@ShyamaSankar
Last active March 17, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShyamaSankar/6f7f7054460ff22c413db8d3ae190cc1 to your computer and use it in GitHub Desktop.
Save ShyamaSankar/6f7f7054460ff22c413db8d3ae190cc1 to your computer and use it in GitHub Desktop.
Create a list of numbers with list comprehension.
# Create list of numbers using for loop.
numbers = []
for number in range(1, 11):
numbers.append(number)
# Rewrite using list comprehension.
# Syntax:
# list_object = [item for_item_in_iterable]
numbers = [number for number in range(1, 11)]
print(numbers) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment