<jupyter_start><jupyter_text>1) Eliminating loops with built-in modules<jupyter_code>poke_types = ['Bug', 'Fire', 'Ghost', 'Grass', 'Water']<jupyter_output><empty_output><jupyter_text>Nested for loop approach<jupyter_code>combos = []
for x in poke_types:
    for y in poke_types:
        if x == y:
            continue
        if ((x,y) not in combos) & ((y,x) not in combos):
            combos.append((x, y))
print(combos)<jupyter_output>[('Bug', 'Fire'), ('Bug', 'Ghost'), ('Bug', 'Grass'), ('Bug', 'Water'), ('Fire', 'Ghost'), ('Fire', 'Grass'), ('Fire', 'Water'), ('Ghost', 'Grass'), ('Ghost', 'Water'), ('Grass', 'Water')]<jupyter_text>Built-in module approach<jupyter_code>from itertools import combinations

combos2 = [*combinations(poke_types, 2)]
print(combos2)<jupyter_output>[('Bug', 'Fire'), ('Bug', 'Ghost'), ('Bug', 'Grass'), ('Bug', 'Water'), ('Fire', 'Ghost'), ('Fire', 'Grass'), ('Fire', 'Water'), ('Ghost', 'Grass'), ('Ghost', 'Water'), ('Grass', 'Water')]<jupyter_text>2) Eliminating loops<jupyter_code>poke_stats = [
    [90, 92, 75, 60],
    [25, 20, 15, 90],
    [65, 130, 60, 75]
]<jupyter_output><empty_output><jupyter_text>For loop approach<jupyter_code>%%timeit
totals = []
for row in poke_stats:
    totals.append(sum(row))
# print(totals)<jupyter_output>1.02 µs ± 86.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>List comprehension<jupyter_code>%timeit totals_comp = [sum(row) for row in poke_stats]
# print(totals_comp)<jupyter_output>1.09 µs ± 136 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>Built-in map() function<jupyter_code>%timeit totals_map = [*map(sum, poke_stats)]
# print(totals_map)<jupyter_output>995 ns ± 15.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)<jupyter_text>3) Efficiently combining, counting and iterating<jupyter_code>names = ['Bulbasur', 'Charmander', 'Squirtle']
hps = [45, 39, 44]
combined = []

for i, pokemon in enumerate(names):
    combined.append((pokemon, hps[i]))
print(combined)<jupyter_output>[('Bulbasur', 45), ('Charmander', 39), ('Squirtle', 44)]<jupyter_text>combining objects with zip- zip returns an zip object that must be unpacked into any datatype<jupyter_code>combined_zip = zip(names, hps)
print(type(combined_zip))
combined_zip_list = [*combined_zip]
print(combined_zip_list)<jupyter_output>[('Bulbasur', 45), ('Charmander', 39), ('Squirtle', 44)]<jupyter_text>counting with loop<jupyter_code>poke_types = ['Grass', 'Dark', 'Fire', 'Fire']

type_counts = {}
for poke_type in poke_types:
    if poke_type not in type_counts:
        type_counts[poke_type] = 1
    else:
        type_counts[poke_type] += 1
print(type_counts)<jupyter_output>{'Grass': 1, 'Dark': 1, 'Fire': 2}<jupyter_text>Efficient approach : collections.Counter()<jupyter_code>from collections import Counter

types_counts = Counter(poke_types)
print(types_counts)<jupyter_output>Counter({'Fire': 2, 'Grass': 1, 'Dark': 1})<jupyter_text>4) Writing better loops- Move calculations outside the loops  Converts tuples to list of list : use map function<jupyter_code>names = ['Pikachu', 'Squirtle', 'Articuno']
legend_status = [False, False, True]
generations = [1,1,1]

poke_data_tuples = []
for poke_tuple in zip(names, legend_status, generations):
    poke_data_tuples.append(poke_tuple)
    
poke_data = [*map(list, poke_data_tuples)]
print(poke_data)<jupyter_output>[['Pikachu', False, 1], ['Squirtle', False, 1], ['Articuno', True, 1]]
{'fraction_non_alphanumeric': 0.17341040462427745, 'fraction_numerical': 0.030635838150289016, 'mean_word_length': 6.411134903640257, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '297473', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1312, 'n_words': 309, 'path': 'notebooks/Efficient Python Codes.ipynb', 'repo_name': 'naikshubham/Miscellaneous-ML-and-Python', 'size': '3460'}