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
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] |
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
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""" |
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
"""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__) |
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
"""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)) |
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
"""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)) |
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 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") |
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
"""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} |
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
"""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 |
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
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""" |
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
"""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 |
OlderNewer