<jupyter_start><jupyter_text>List comprehensions  Transform 1 dimensional list<jupyter_code>x1 = [1,2,3,4,5]

x2 = [10*x for x in x1]
x2<jupyter_output><empty_output><jupyter_text>Filter out values with 'if'<jupyter_code>x3 = [x for x in x2 if x>10 and x<50]
x3<jupyter_output><empty_output><jupyter_text>Transform values with 'if' and 'else'<jupyter_code>x4 = [0 if x < 30 else 1 for x in x3]
x4<jupyter_output><empty_output><jupyter_text>Create and fill a list of lists<jupyter_code>x5 = [[] for x in range(5)]
x5
x5[-1].append(3)
x5
x5.extend([[6,7]])
x5<jupyter_output><empty_output><jupyter_text>Flatten a 2-dimensional array<jupyter_code>dat = [[1,2,3],[4,5,6]]
x6 = [val for row in dat for val in row]
x6<jupyter_output><empty_output><jupyter_text>Find all the indexes where list values changed in sequential order<jupyter_code>aa = [1,1,2,3,3,3,4]
x7 = [0] + [i+1 for i,x in enumerate(aa[1:]) if x != aa[i]]
x7
# Equivalent to:
x8 = [0] + [i+1 for i,x in enumerate(aa[:-1]) if x != aa[i+1]]
x8<jupyter_output><empty_output><jupyter_text>Get value pairs from two lists using 'zip'<jupyter_code>xx = [11,12,13]
yy = [21,22,23]
xy1 = list(zip(xx, yy))
xy1<jupyter_output><empty_output><jupyter_text>Iterate 2 lists to get all possible pair tuples<jupyter_code># Equivalent to using 2 for loops as follows:
# for x in xx:
#     for y in yy:
#        xy = (x,y)

xy2 = [(x,y) for x in xx for y in yy]
xy2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14683368128044538, 'fraction_numerical': 0.05706332637439109, 'mean_word_length': 6.049019607843137, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 34, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '69738', 'license': 'MIT', 'n_tokens_mistral': 658, 'n_tokens_neox': 633, 'n_words': 160, 'path': 'docs/notebooks/misc/list_comprehensions.ipynb', 'repo_name': 'gal-a/blog', 'size': '1437'}