List comprehension in Python is a compact way of creating a list from a sequence. It is a single line of code that is also meant to be easy to read and understand.
Here is the basic syntax of list comprehension in Python:
`new_list = [expression for member in iterable]`
`expression` is the member itself, a call to a method, or any other valid expression that returns a value. In the code above, expression is the current member in the iterable.
`member` is the object or value in the list or iterable. In the code above, the member value is the current member in the iterable.
`iterable` is a sequence, collection, or an iterator object to be traversed.
Here is an example of a list comprehension:
\`numbers = [1, 2, 3, 4]
squares = [number\*\*2 for number in numbers]
print(squares)\`
Output:
`[1, 4, 9, 16]`
The list, squares, is equal to the list of each number in the numbers list squared or raised to the power of 2. The for statement sets up number as its reference to each member right as it traverses through the list, numbers.
To add some condition to the list comprehension, you can add an if statement:
`squares = [number**2 for number in numbers if number > 2]`
`print(squares)`
Output:
`[9, 16]`
In this case, python will square the number if it’s greater than 2.