<jupyter_start><jupyter_text>Intermediate Python  List Comprehensions  Simple examples<jupyter_code># raise power of two
[number**2 for number in range(10)]
# modulo 2
[number%2 for number in range(10)]
[number for number in range(10) if number%2==0]<jupyter_output><empty_output><jupyter_text>Basic forms [ *expr* for *val* in *collection*]- This parallels the structure of map- **expr** is the function we apply to the collection- we iterate over the collection, applying the expression to each *val* in the collection[ *expr* for *val* in *collection* if ][ *expr* for *val1* in *collection1* for *val2* in *val2* in *collection2*]   More Examples - Build list of last name initial from list of last names- Build list of last names which only begin with a vowel- Build list of initials from list of full names<jupyter_code>last_names = ['Austen','Diderot','Woolf','Melville','Pessoa','Goethe','Akhmatova']
full_names = ['<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>']
# list of last name initial from list of last names
[name[0] for name in last_names]
# length of each last name from list of last names
[len(name) for name in last_names]
# last names that begin with vowels
[name for name in last_names if name[0] in 'AEIOU']
# first names
[name.split()[0] for name in full_names]
last_names = ['Austen','Diderot','Woolf','Melville','Pessoa','Goethe','Akhmatova']
full_names = ['<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>', '<NAME>']
#initials
[name.split()[0][0]+name.split()[1][0] for name in full_names]
suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
ranks = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
# cards in a card deck
cards = [(suit,rank) for suit in suits for rank in ranks]
cards[0:3]<jupyter_output><empty_output><jupyter_text>Dictionary Comprehension (Bonus)<jupyter_code>dict1 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
{k:v*2 for (k,v) in dict1.items()}
dict1 = {'Melville': ['<NAME>', 'Type','Omoo','Billy Budd'], 'Austen': ['Pride and Prejudice','Emma']}
{k:v[0] for (k,v) in dict1.items()}
{k:len(v) for (k,v) in dict1.items()}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18486997635933805, 'fraction_numerical': 0.021749408983451537, 'mean_word_length': 6.557142857142857, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '210222', 'license': 'MIT', 'n_tokens_mistral': 762, 'n_tokens_neox': 745, 'n_words': 246, 'path': 'IntermediatePython/Comprehension.ipynb', 'repo_name': 'walshkc/lak19', 'size': '2230'}