Skip to content

Instantly share code, notes, and snippets.

View FerdinaKusumah's full-sized avatar
🧑‍🚀
✌🏻

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / transpose_2d_list.py
Created December 5, 2019 01:29
Transpose 2d list
"""Transpose 2d list"""
a = [["a", "b"], ["c", "d"], ["e", "f"]]
transposed = zip(*a)
to_list = list(transposed)
print("Transpose list from {} to {}".format(a, to_list))
# Transpose list from [['a', 'b'], ['c', 'd'], ['e', 'f']] to [('a','c', 'e'), ('b', 'd', 'f')]
@FerdinaKusumah
FerdinaKusumah / chain_comparison.py
Created December 5, 2019 01:28
Chain Comparison
"""Chain comparison"""
min_num = 5
max_num = 10
search = 3
is_middle = search < min_num < max_num
is_name = search == 3 < min_num < max_num
print("is number {} in middle {} and {} ? {}".format(search, min_num, max_num, is_middle))
# is number 3 in middle 5 and 10 ? True
@FerdinaKusumah
FerdinaKusumah / ternary.py
Last active December 5, 2019 01:50
Ternary Operator
"""Ternary operator"""
a = 15
b = 12
is_a_greater = False if b > a else True
print("Is a is greater than b ? {}".format(is_a_greater))
# Is a is greater than b ? True
@FerdinaKusumah
FerdinaKusumah / chain_func_call.py
Created December 5, 2019 01:26
Chaining function call
"""Chain function call"""
def sum_product(a, b):
return a + b
def reduce_product(a, b):
return a - b
need_to_sum = True
a = 10
b = 4
@FerdinaKusumah
FerdinaKusumah / copy_variable.py
Created December 5, 2019 01:25
Copy or Clone Variable
from copy import deepcopy
"""Clone or copy object to new ones"""
a = [1, 2, 3, 4]
b = deepcopy(a)
"""Try to adding new value in list a"""
a.append(5)
"""Now print this value"""
@FerdinaKusumah
FerdinaKusumah / get_dict_key_with_default_value.py
Created December 5, 2019 01:23
Get dict key with default value
"""Get dictionary value and set default value when not exists"""
a = dict()
a['apple'] = 10
a['banana'] = 12
print(a)
# {'apple': 10, 'banana': 12}
"""Print print wheneather not exists value and set default value"""
print(a.get('pear', 5))
# 5
@FerdinaKusumah
FerdinaKusumah / sort_dict.py
Created December 5, 2019 01:22
Sort Dictionary
"""Sort dict by keys or values"""
a = dict()
a['banana'] = 5
a['apple'] = 2
a['orange'] = 3
"""Before sort"""
print("Before sort = {}".format(a))
# Before sort = {'banana': 5, 'apple': 2, 'orange': 3}
@FerdinaKusumah
FerdinaKusumah / for_else.py
Last active December 5, 2019 01:51
For else
"""For else"""
a = [1, 2, 3, 4]
# Search number in list
search_num = 10
for num in a:
if num > search_num:
break
else:
print("Sorry but number you're trying to find is not found")
@FerdinaKusumah
FerdinaKusumah / comma_separated.py
Last active December 5, 2019 01:51
Create comma separated from list
"""List string"""
a = ["a", "b", "c", "d"]
string_separated = ",".join(a)
print("Join string from list to {}".format(string_separated))
# Join string from list to a,b,c,d
"""List Number"""
b = [1, 2, 3, 4, 5]
number_separated = ",".join(map(str, b))
print("Join number from list to {}".format(number_separated))
@FerdinaKusumah
FerdinaKusumah / merge_dict.py
Last active December 5, 2019 01:51
Merge Dictionary
"""Merge dict"""
a = {"apple": 10}
b = {"banana": 3}
example1 = {**a, **b}
print("Merge dict 1 to {}".format(example1))
# Merge dict 1 to {'apple': 10, 'banana': 3}
example2 = dict(a.items() | b.items())
print("Merge dict 2 to {}".format(example2))