Python
Beginner
Estimated Duration: 12 min
Python: Fast Data Filtering with List Comprehensions
Learn how to write elegant, idiomatic, and high-performance Python by replacing clunky loops with list comprehensions.
Python List Comprehensions
List comprehensions provide a concise way to create lists based on existing lists. They are faster and more readable than standard for-loops.
Syntax
python
[expression for item in iterable if condition]
Example
python
numbers = [1, 2, 3, 4, 5, 6]
evens = [n for n in numbers if n % 2 == 0]
print(evens) Output: [2, 4, 6]
Your Task
Given a list of domain names, create a list comprehension that extracts only the domains that end with .com and converts them to lowercase.
Target Output
["seoffix.com", "github.com", "openai.com"]
Need a Hint?
- Use d.lower() on each item
- Check with .endswith(".com")
- Combine into [d.lower() for d in raw_domains if ...]
solution.py
Terminal Output
Ready
Click "Run Code & Test" to execute your solution.