<jupyter_start><jupyter_text>List Comprehension vs For Loop- Suppose, we want to separate the letters of the word `human` and add the letters as items of a list. The first thing that comes in mind would be using `for loop`. __Iterating through a string Using for Loop__<jupyter_code>h_letters = []

for letter in 'human':
    h_letters.append(letter)

print(h_letters)<jupyter_output>['h', 'u', 'm', 'a', 'n']<jupyter_text>However, Python has an easier way to solve this issue using List Comprehension. List comprehension is an elegant way to define and create lists based on existing lists. __Iterating through a string Using List Comprehension__<jupyter_code>h_letters = [ letter for letter in 'human']
print(h_letters)<jupyter_output>['h', 'u', 'm', 'a', 'n']<jupyter_text>__Syntax of List Comprehension__`[expression for item in list]`  List Comprehensions vs Lambda functions- List comprehensions aren’t the only way to work on lists. Various built-in functions and __lambda functions__ can create and modify lists in less lines of code. __Using Lambda funtions inside List__<jupyter_code>h_lettres = list(map(lambda x: x, 'human'))
h_letters<jupyter_output><empty_output><jupyter_text>Conditionals in List Comprehension- List comprehensions can utilize conditional statement to modify existing list (or other tuples). We will create list that uses mathematical operators, integers, and `range()`. __Using if with List Comprehension__<jupyter_code>number_list = [x for x in range(20) if x % 2 == 0]
number_list
list(range(20))<jupyter_output><empty_output><jupyter_text>__Nested IF with List Comprehension__<jupyter_code>num_list = [y for y in range(100) if y % 2 == 0 if y % 5 == 0]
num_list<jupyter_output><empty_output><jupyter_text>__if...else With List Comprehension__<jupyter_code>obj = ['Even' if i % 2 == 0 else 'Odd' for i in range(10)]
obj<jupyter_output><empty_output><jupyter_text>Nested Loops in List Comprehension- Suppose, we need to compute transpose of a matrix which requires nested for loop. Let’s see how it is done using normal for loop first. __Transpose of Matrix using Nested Loops__<jupyter_code>matrix = [[1, 2],[3,4],[5,6],[7,8]]
transposed = []

for i in range(2):
    transposed_row = []
    for row in matrix:
        transposed_row.append(row[i])
    transposed.append(transposed_row)
transposed<jupyter_output><empty_output><jupyter_text>__Transpose of a Matrix using List Comprehension__<jupyter_code>matrix = [[1, 2],[3,4],[5,6],[7,8]]

transpose = [[row[i] for row in matrix] for i in range(2)]
transpose<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10023310023310024, 'fraction_numerical': 0.013597513597513598, 'mean_word_length': 6.074175824175824, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '395316', 'license': 'MIT', 'n_tokens_mistral': 877, 'n_tokens_neox': 830, 'n_words': 291, 'path': 'Step_1_Introduction to Python/Python Programming_Intermediate/List Comprehension.ipynb', 'repo_name': 'augustine0890/Data-Analyst', 'size': '2574'}