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)] |
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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# For loop to create a list with squares of all odd numbers in the original list. | |
odd_squares = [] | |
for number in numbers: | |
if number % 2 == 1: | |
odd_squares.append(number * number) | |
# Rewrite using 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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# For loop to create a list with squares of all odd numbers | |
# and doubles of all even numbers in the original list. | |
modified_numbers = [] | |
for number in numbers: | |
if number % 2 == 1: | |
modified_numbers.append(number * number) | |
else: |
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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
def modifier(number): | |
""" | |
Returns 0 if number is a multiple of 2, 1 if number is a multiple of 3 | |
and otherwise, the number itself. | |
""" | |
return 0 if number % 2 == 0 else 1 if number % 3 == 0 else number |
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
# Our list of lists. | |
matrix = [[1,2,3], [4,5,6], [7,8,9]] | |
# For loop to flatten the matrix. | |
flattened_list = [] | |
for each_list in matrix: | |
for number in each_list: | |
flattened_list.append(number) | |
# Rewrite using 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
substrings_list = ["an", "the"] | |
strings_list = ["an apple", "the tree", "all trees", "an apple in the tree"] | |
# For loop to create a list of (substring, string) tuples from two lists. | |
# First is a list substrings and the second is a list of strings to be parsed. | |
tuple_list = [] | |
for string in strings_list: | |
for substring in substrings_list: | |
if substring in string: | |
tuple_list.append((substring, string)) |
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] |
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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# For loop to create a new list where we substitute all multiples of 2 by 0, | |
# multiples of 3(which are not multiples of 2) by 1 and leave the rest as it is. | |
modified_numbers = [] | |
for number in numbers: | |
if number % 2 == 0: | |
modified_numbers.append(0) | |
elif number % 3 == 0: |
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
# Our list of lists. | |
matrix_1 = [[1,1,1], [2,2,2], [3,3,3]] | |
matrix_2 = [[4,4,4], [5,5,5], [6,6,6]] | |
# Matrix addition with for loop. | |
# Assuming that the matrices are of the same dimensions | |
matrix_sum = [] | |
for row in range(len(matrix_1)): | |
matrix_sum.append([]) | |
for column in range(len(matrix[0])): |
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 an empty list using square brackets. | |
numbers = [] | |
print(numbers) # Output: [] | |
# Create an empty list using list(). | |
numbers = list() | |
print(numbers) # Output: [] | |
# Create a list of numbers. | |
numbers = [1, 2, 3] |
OlderNewer