Last active
March 17, 2019 03:28
-
-
Save ShyamaSankar/6f7f7054460ff22c413db8d3ae190cc1 to your computer and use it in GitHub Desktop.
Create a list of numbers with list comprehension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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