<jupyter_start><jupyter_text>List ComprehensionList are one of the core data structures in Python 🐍. Consequently, the Python programming language containsspecial features for working with lists. One of this feature is list comprehension. According to the Python [documentation](https://docs.python.org/3/glossary.htmlterm-list-comprehension) list comprehension is > A compact way to process all or part of the elements in a sequence and return a list with the results. Using list comprehension it is possible to:- create a new list by performing an operation on the elements of an existing sequence- create a new list by filtering the elements of an existing sequenceIn this unit those two possibilities are introduced using examples. Creating listsThe first usage of list comprehension is to perform an operation on all elements of a sequence. As an example consider alist containing the numbers from 1 to 20. In order to create a list containing the squares of these numbers the followingPython code can be used.<jupyter_code>numbers = list(range(1, 21))

squares = []

for number in numbers:
    squares.append(number * number)

print("The list of squares is:", squares)<jupyter_output><empty_output><jupyter_text>In this example a list of numbers is created from a [`range`](https://docs.python.org/3/library/stdtypes.htmlrange).Next a `for`-loop is used to calculate the square of every number in the list and append it to the list of squaresstored in the variable `squares`.Using list comprehension the same functionality can be implemented much more concise and easier to read. This is shownin the following cell.<jupyter_code>numbers = list(range(1, 21))
squares = [x * x for x in numbers]

print("The list of squares is:", squares)<jupyter_output><empty_output><jupyter_text>A list comprehension creates a new list. Therefore, the main statements of the list comprehension are enclosed in square brackets. Inside the square brackets first the statement to create a list element is given.In the example above this is `x * x`. Next, the sequence, that is the basis of the list comprehension is specified. Inthe example above `for x in numbers` states that in turn every element of the list numbers is assigned to the variable `x`. In summary, the one line preforms the same operation as the for loop.If only the list of squares is needed the `range` could be used directly in the list comprehension.<jupyter_code>squares = [x * x for x in range(1, 21)]
print("The list of squares is:", squares)<jupyter_output><empty_output><jupyter_text>Remember the exercise to generate 100 random integer numbers? This is also possible using list comprehension.<jupyter_code>from random import randint

random_numbers = [randint(1, 10) for _ in range(100)]
print("The list of random numbers is:", random_numbers)<jupyter_output><empty_output><jupyter_text>To generate the random numbers, the `randint()` function from the `random` library is used. In order to create 100random numbers, a `range(100)` is used. However, in this example the values of the range are not used. Therefore, a `_`is used instead of a variable. Using multiple lists in a list comprehensionIt is even possible to use multiple lists inside a list comprehension. For example, to create a list containing all thecombinations of two lists the following list comprehension can be used. This is much more concise and therefore morereadable as the standard approach using two nested `for`-loops.<jupyter_code>letters = ["A", "B", "C"]
numbers = [1, 2, 3]

combinations = [l + str(n) for l in letters for n in numbers]

print("The possible combinations of letters and numbers are:", combinations)<jupyter_output><empty_output><jupyter_text>Filtering listsAs mentioned in the introduction, list comprehensions are also very useful in order to filter existing sequences. As anexample consider a list containing songs and the number of times these songs were played. In order to create a list withall songs that were played at least 30 times, the following Python 🐍 code could be used.<jupyter_code>songs = [
    ("Ace of Spades", 99),
    ("Anarchy in the UK", 51),
    ("Blitzkrieg Bop", 17),
    ("Blue Train", 42),
    ("Dirty", 23),
    ("No Sleep Til Brooklyn", 15),
    ("Paranoid", 33),
]

favorite_songs = []

for song in songs:
    if song[1] >= 30:
        favorite_songs.append(song)

print("The songs that were played at least 30 times are:", favorite_songs)<jupyter_output><empty_output><jupyter_text>Using list comprehension the task can be solved as shown below.<jupyter_code>songs = [
    ("Ace of Spades", 99),
    ("Anarchy in the UK", 51),
    ("Blit<NAME>", 17),
    ("Blue Train", 42),
    ("Dirty", 23),
    ("No Sleep Til Brooklyn", 15),
    ("Paranoid", 33),
]

favorite_songs = [s for s in songs if s[1] >= 30]

print("The songs that were played at least 30 times are:", favorite_songs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07777097366809553, 'fraction_numerical': 0.014696876913655848, 'mean_word_length': 4.896510228640192, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '265659', 'license': 'CC0-1.0', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1293, 'n_words': 664, 'path': 'week_6/week_6_unit_7_listcompr_notebook.ipynb', 'repo_name': 'ceedee666/opensap_python_intro', 'size': '4903'}