Skip to content

Instantly share code, notes, and snippets.

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

Ferdina Kusumah FerdinaKusumah

🧑‍🚀
✌🏻
View GitHub Profile
@FerdinaKusumah
FerdinaKusumah / flatten_list.py
Last active December 5, 2019 01:12
Flatten LIst
from itertools import chain
"""Flatten list from nested list"""
a = [[1, 2], [3, 4], [5, 6]]
flat_list = list(chain(*a))
print("Flatten list = {}".format(flat_list))
# Flatten list = [1, 2, 3, 4, 5, 6]
@FerdinaKusumah
FerdinaKusumah / remove_duplicate.py
Created December 5, 2019 01:15
Remove Duplicate List Or Dict
from collections import OrderedDict
"""Remove duplicate in list"""
a = [1, 1, 3, 4, 6, 7, 4, 6, 2, 1]
remove_duplicate_list = list(set(a))
print(remove_duplicate_list)
# [1, 2, 3, 4, 6, 7]
"""Remove duplicate in dict and keep order"""
@FerdinaKusumah
FerdinaKusumah / find_min_max.py
Created December 5, 2019 01:17
Find Min Max Index In LIst
"""Find index min and max in list"""
a = [4, 3, 7, 2, 12]
def get_min(arr: list) -> int:
return min(range(len(arr)), key=arr.__getitem__)
def get_max(arr: list) -> int:
return max(range(len(arr)), key=arr.__getitem__)
@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))
@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 / 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 / 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 / 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 / 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 / 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