Junior — Middle
Have you used list comprehension expressions when working with Python?
sobes.tech AI
Answer from AI
Yes, list comprehension expressions in Python are widely used for creating new lists based on existing ones, with the ability to filter and transform elements.
Example:
# Create a list of squares of numbers from 0 to 9
squares = [x**2 for x in range(10)]
# Filter only even squares
even_squares = [x for x in squares if x % 2 == 0]
print(squares) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(even_squares) # [0, 4, 16, 36, 64]
List comprehensions make the code more compact and readable compared to using loops and conditional statements.