Hillary Chege
Crafting Digital Excellence

Code Snippets

Useful code examples and solutions I've collected. Feel free to use and learn from them!

python_list_comprehension.py ×

// Python List Comprehension

// Create lists using comprehension syntax

# Basic list comprehension
squares = [x**2 for x in range(10)]
print(squares)

# With condition
even_squares = [x**2 for x in range(10) if x % 2 == 0]
print(even_squares)

# Nested comprehension
matrix = [[i+j for j in range(3)] for i in range(3)]
print(matrix)