- 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:
squares = [] for x in range(1, 6): squares.append(x**2)
squares = [x**2 for x in range(1, 6)]
- 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
evens = [] for x in range(1, 11): if x % 2 == 0: evens.append(x)
evens = [x for x in range(1, 11) if x % 2 == 0]
Example 3:
- Reverse each string in a Tuple.
original_tuple = ('hello', 'world', 'python') reversed_strings = [] for s in original_tuple: reversed_strings.append(s[::-1])
original_tuple = ('hello', 'world', 'python') reversed_strings = [s[::-1] for s in original_tuple]
Example 4:
- Toggle the case of each character in a String.
input_string = 'Python List Comprehension' toggled_string = '' for char in input_string: if char.islower(): toggled_string += char.upper() else: toggled_string += char.lower()
input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Example 5:
- Toggle the case of each character in a String.
input_string = 'Python List Comprehension' toggled_string = '' for char in input_string: if char.islower(): toggled_string += char.upper() else: toggled_string += char.lower()
input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Example 6:
- Nested IF with List Comprehension.
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)
input_string = 'Python List Comprehension' toggled_string = ''.join([char.upper() if char.islower() else char.lower() for char in input_string])
Example 7:
- Python List Comprehension using If-else.
numbers = [1, 2, 3, 4, 5, 6] results = [] for x in numbers: if x % 2 == 0: results.append("Even") else: results.append("Odd")
numbers = [1, 2, 3, 4, 5, 6] results = ["Even" if x % 2 == 0 else "Odd" for x in numbers]
Example 8:
- Flatten a Nested List.
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)
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.
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)
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]
Example 10:
- Cross Product of Two Lists
list1 = [1, 2, 3] list2 = ['A', 'B', 'C'] cross_product = [] for x in list1: for y in list2: cross_product.append((x, y))
list1 = [1, 2, 3] list2 = ['A', 'B', 'C'] cross_product = [(x, y) for x in list1 for y in list2]
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:
even_squares = [] for x in range(1, 11): if x % 2 == 0: even_squares.append((lambda x: x**2)(x))
even_squares = [(lambda x: x**2)(x) for x in range(1, 11) if x % 2 == 0]
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()
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.
Top 10 Common Words: text: 3 the: 3 is: 2 to: 2 analysis: 2 this: 1 an: 1 example: 1 file: 1
- Check below link for complete code
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.
More Refrence Link for Python Concepts