Monday, October 30, 2023

Mastering List Comprehensions in Python: A Comprehensive Guide

  • List comprehensions are a powerful and concise way to create lists in Python, but when combined with lambda functions, they become even more versatile. 
  • In this post, we'll explore the basics of Python list comprehensions, and showcase examples. 🐍

The Basics of List Comprehensions

  • List comprehensions are a way to create a new list by applying an expression to each item in an iterable and optionally filtering items based on a condition. 
  • The basic structure of a list comprehension looks like this.

new_list = [expression for item in iterable if condition]
  • Expression: The operation or calculation you want to perform on each item.
  • Item: Represents each element in the iterable.
  • Iterable: The source from which you want to create the new list.
  • Condition (optional): An expression that filters elements based on a specified condition.

Example of List Comprehensions

Example 1: 

  • Say we want to create a list of the squares of numbers from 1 to 5 using a list comprehension:
Without List Comprehensions:

squares = [] for x in range(1, 6): squares.append(x**2)
With List Comprehensions:

squares = [x**2 for x in range(1, 6)]
Output :  [1, 4, 9, 16, 25]

Example 2:
  • List comprehensions can include a condition to filter the items. Here's an example where we create a list of even numbers between 1 and 10
Without List Comprehensions:

evens = [] for x in range(1, 11): if x % 2 == 0: evens.append(x)
With List Comprehensions:

evens = [x for x in range(1, 11) if x % 2 == 0]
Output :  [2, 4, 6, 8, 10]

Example 3:

  • Reverse each string in a Tuple.
Without List Comprehensions:

original_tuple = ('hello', 'world', 'python') reversed_strings = [] for s in original_tuple: reversed_strings.append(s[::-1])
With List Comprehensions:

original_tuple = ('hello', 'world', 'python') reversed_strings = [s[::-1] for s in original_tuple]
Output :  ['olleh', 'dlrow', 'nohtyp']

Example 4:

  • Toggle the case of each character in a String.
Without List Comprehensions:

input_string = 'Python List Comprehension' toggled_string = '' for char in input_string: if char.islower(): toggled_string += char.upper() else: toggled_string += char.lower()
With List Comprehensions:

input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Output :  'pYTHON lIST cOMPREHENSION'

Example 5:

  • Toggle the case of each character in a String.
Without List Comprehensions:

input_string = 'Python List Comprehension' toggled_string = '' for char in input_string: if char.islower(): toggled_string += char.upper() else: toggled_string += char.lower()
With List Comprehensions:

input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Output :  'pYTHON lIST cOMPREHENSION'

Example 6:

  • Nested IF with List Comprehension.
Without List Comprehensions:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] evens = [] for x in numbers: if x % 2 == 0: if x % 4 == 0: evens.append(x)
With List Comprehensions:

input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Output :  '[4, 8]'

Example 7:

  • Python List Comprehension using If-else.
Without List Comprehensions:

numbers = [1, 2, 3, 4, 5, 6] results = [] for x in numbers: if x % 2 == 0: results.append("Even") else: results.append("Odd")
With List Comprehensions:

numbers = [1, 2, 3, 4, 5, 6] results = ["Even" if x % 2 == 0 else "Odd" for x in numbers]
Output :  '['Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even']'

Example 8:

  • Flatten a Nested List.
Without List Comprehensions:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened_list = [] for sublist in nested_list: for item in sublist: flattened_list.append(item)
With List Comprehensions:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened_list = [item for sublist in nested_list for item in sublist]

Output :  [1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 9:

  • Extract Unique Elements from a List.
Without List Comprehensions:

input_list = [1, 2, 2, 3, 4, 4, 5, 6, 6] unique_elements = [] for item in input_list: if item not in unique_elements: unique_elements.append(item)
With List Comprehensions:

input_list = [1, 2, 2, 3, 4, 4, 5, 6, 6] unique_elements = [item for item in input_list if input_list.count(item) == 1]
Output :  [1, 3, 5]

Example 10:

  • Cross Product of Two Lists
Without List Comprehensions:

list1 = [1, 2, 3] list2 = ['A', 'B', 'C'] cross_product = [] for x in list1: for y in list2: cross_product.append((x, y))
With List Comprehensions:

list1 = [1, 2, 3] list2 = ['A', 'B', 'C'] cross_product = [(x, y) for x in list1 for y in list2]
Output :  [(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'), (2, 'C'), (3, 'A'), (3, 'B'), (3, 'C')]

Combining List Comprehensions and Lambda Functions

  • Lambda functions, also known as anonymous functions, are small, one-line functions that don't require a name. They are often used for simple operations and can be defined using the lambda keyword. The basic syntax is as follows:

lambda arguments: expression

  • Let's create a lambda function that squares a number:

Without Lambda Functions:

def square(x): return x**2

With Lambda Functions:

square = lambda x: x**2

  • Now, let's explore how list comprehensions can be enhanced with lambda functions. Suppose you want to create a list of the squares of even numbers between 1 and 10 using a lambda function:

Without List Comprehensions:

even_squares = [] for x in range(1, 11): if x % 2 == 0: even_squares.append((lambda x: x**2)(x))
With List Comprehensions:

even_squares = [(lambda x: x**2)(x) for x in range(1, 11) if x % 2 == 0]
Output : [4, 16, 36, 64, 100]

Exercise_1 - Text Analysis Tool

  • Create a Python program that reads multiple text files, processes the text, and finds the most common words in the collection of files.

Requirements:

  • The program should take a directory as input and analyze all text files within that directory.
  • It should read the content of each file, tokenize the text into words, and store them in a list.
  • Common words like "the," "and," "is," etc., should be excluded from the analysis.
  • The program should then count the frequency of each word.
  • Finally, it should display the top N most common words and their frequencies.

import os
import string

def read_text_file(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text
except FileNotFoundError:
print(f"File not found: {file_path}")
return ""

def process_text(text):
# Remove punctuation and convert to lowercase
translator = str.maketrans('', '', string.punctuation)
text = text.lower()
text = text.translate(translator)
# Tokenize the text
words = text.split()
return words

def analyze_directory(directory, num_common_words):
word_frequency = {}

# List comprehension to read and process text files in the directory
texts = [process_text(text) for text in (read_text_file(os.path.join(directory, file)) for file in os.listdir(directory) if file.endswith(".txt"))]

# List comprehension to count word frequencies
for words in texts:
for word in words:
if word not in word_frequency:
word_frequency[word] = 1
else:
word_frequency[word] += 1

# Sort the word frequency dictionary by frequency in descending order
sorted_word_frequency = {k: v for k, v in sorted(word_frequency.items(), key=lambda item: item[1], reverse=True)}

# List comprehension to get the top N common words
top_words = [(word, freq) for word, freq in list(sorted_word_frequency.items())[:num_common_words]]

return top_words

def main():
# Get the directory of the currently running script
script_directory = os.path.dirname(os.path.abspath(__file__)) if __file__ is not None else os.path.realpath(__file__)

num_common_words = 10 # Change this value to the desired number of common words

top_words = analyze_directory(script_directory, num_common_words)

print(f"Top {num_common_words} Common Words:")
for word, frequency in top_words:
print(f"{word}: {frequency}")

if __name__ == "__main__":
main()
Text_File.txt

This is an example text file. It contains multiple lines and words. The purpose is to demonstrate the functionality of the text analysis tool. Please feel free to modify or use your own text files for analysis.
Output

Top 10 Common Words: text: 3 the: 3 is: 2 to: 2 analysis: 2 this: 1 an: 1 example: 1 file: 1

Exercise_2 - Python Exceptions Tracker Application

To-Do List Application: Create a command-line to-do list application that allows users to manage their tasks.

Requirements:

  • The program should support adding tasks to the to-do list.
  • Users should be able to remove tasks by providing the task's index.
  • The program should display the current list of tasks.
  • The user should be able to mark tasks as completed.
  • Completed tasks should be displayed separately from uncompleted tasks.
  • The user should be able to clear all completed tasks.
  • Users can quit the application.

    You may also like

    Kubernetes Microservices
    Python AI/ML
    Spring Framework Spring Boot
    Core Java Java Coding Question
    Maven AWS