Created
March 17, 2019 08:16
-
-
Save ShyamaSankar/7d0d201f2623df7b09638515d9ba78d7 to your computer and use it in GitHub Desktop.
List comprehension with nested if conditions
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
# For loop to create a list with all even multiples of 5 (nothing but multiples of 10 :/). | |
filtered_list = [] | |
for number in range(1, 101): | |
if number % 2 == 0: | |
if number % 5 == 0: | |
filtered_list.append(number) | |
# Rewrite using list comprehension. | |
# Syntax: | |
# list_object = [expression_on_item for_item_in_iterable if_condition_1_on_item if_condition_2_on_item] | |
filtered_list = [number for number in range(1, 101) if number % 2 == 0 if number % 5 == 0] | |
print(filtered_list) # Output: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment