Skip to content

Instantly share code, notes, and snippets.

View ShyamaSankar's full-sized avatar

Shyama Sankar Vellore ShyamaSankar

View GitHub Profile
@ShyamaSankar
ShyamaSankar / list_of_numbers_list_comp.py
Last active March 17, 2019 03:28
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)]
@ShyamaSankar
ShyamaSankar / list_comprehension_with_expression.py
Last active November 22, 2022 09:05
List comprehension to double numbers in a list
# Original list of numbers.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# For loop to double the numbers in a list.
doubled_numbers = []
for number in numbers:
doubled_numbers.append(number * 2)
# Rewrite using list comprehension.
# Syntax:
# 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.
@ShyamaSankar
ShyamaSankar / list_comprehension_with_if_else_condition.py
Last active March 17, 2019 05:54
List comprehension with an if-else condition.
# 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:
@ShyamaSankar
ShyamaSankar / list_comprehension_with_if_elif_else_ladder.py
Created March 17, 2019 06:06
List comprehension with an if-elif-else ladder
# 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:
@ShyamaSankar
ShyamaSankar / list_comprehension_with_method.py
Last active March 17, 2019 06:33
Method within a list comprehension.
# 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
@ShyamaSankar
ShyamaSankar / list_comprehension_with_nested_if.py
Created March 17, 2019 08:16
List comprehension with nested if conditions
# 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]
@ShyamaSankar
ShyamaSankar / list_comprehension_nested_for_loop.py
Created March 17, 2019 08:53
Nested for loop into a list comprehension
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))
@ShyamaSankar
ShyamaSankar / list_comprehension_flatten_list_of_lists.py
Created March 17, 2019 09:13
Flatten list of list using list comprehension
# 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.
@ShyamaSankar
ShyamaSankar / nested_list_comprehension.py
Created March 17, 2019 09:45
Matrix addition using nested list comprehension and nested for loops.
# 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])):