<jupyter_start><jupyter_text>List comprehensions* Allows to construct a list in a single line of code* List : Collection of data surrounded by brackets and the elements and seperated by commas* List Compreshension : Sorounded by brackets with for loops and iif statements (Enter expression followed by for loop)![](images/list_comprehensions.jpg)<jupyter_code># Simple examples of list comprehension
# List of the squares of the first 100 positive integers
squares=[]
for items in range(1,101):
    squares.append(items**2)
print(squares)
 [items**2 for items in range(1,101)]
#Divde the first one hundred integers by 5
remainders=[x**2 % 5 for x in range(1,101)]
print(remainders)
# List comprehensions with if clause
# Get all the movies that would start with letter s
movies =["star wars", "james bond","star wars 1"]
print(movies[0])
filtered= [movie for movie in movies if movie.startswith("s")]
print(filtered)
# Find movies released before 2000
# This is stored in a tuple
movies =[("star wars 1",1978),("star wars 2",1990),("star wars 3",2001)]
filtered=[title for (title,year) in movies if year < 2000]
print(filtered)<jupyter_output>['star wars 1', 'star wars 2']
{'fraction_non_alphanumeric': 0.10400682011935208, 'fraction_numerical': 0.040068201193520885, 'mean_word_length': 5.946745562130178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '637647', 'license': 'MIT', 'n_tokens_mistral': 399, 'n_tokens_neox': 343, 'n_words': 151, 'path': 'basics/ipynb/list_comprehensions.ipynb', 'repo_name': 'PRkudupu/Algo-python', 'size': '1173'}