List Comprehension

List comprehension in Python is a concise and powerful way to create lists. It allows you to generate new lists by applying an expression to each item in an iterable, optionally filtering items with a condition. List comprehensions are often used to replace loops, making the code more readable and concise.

Basic Syntax

The basic syntax of a list comprehension is:

# expression for item in iterablea
# e.g.
new_list = [new_item for item in list]
  • expression: The value to include in the new list.
  • item: The variable that takes each value in the iterable.
  • iterable: The collection of items to iterate over.

Examples

  1. Creating a List of Squares:

    • Example:
      squares = [x**2 for x in range(10)]
      # squares is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
      
  2. Filtering with List Comprehension:

    • You can include an if statement to filter items.
    • Example:
      even_squares = [x**2 for x in range(10) if x % 2 == 0]
      # even_squares is [0, 4, 16, 36, 64]
      
  3. Nested List Comprehension:

    • List comprehensions can be nested to handle complex cases like creating a matrix.
    • Example:
      matrix = [[row * col for col in range(5)] for row in range(3)]
      # matrix is [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
      
  4. Flattening a List of Lists:

    • Flatten a two-dimensional list into a one-dimensional list.
    • Example:
      nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
      flattened = [item for sublist in nested_list for item in sublist]
      # flattened is [1, 2, 3, 4, 5, 6, 7, 8, 9]
      
  5. Using Functions in List Comprehension:

    • Apply a function to each element in the list.
    • Example:
      def square(x):
          return x * x
      
      squares = [square(x) for x in range(10)]
      # squares is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
      

List Comprehension vs. Loops

List comprehensions are often preferred over loops for their conciseness. However, they should be used when they improve readability and not just for the sake of brevity.

  • Using a Loop:

    squares = []
    for x in range(10):
        squares.append(x**2)
    # squares is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    
  • Using List Comprehension:

    squares = [x**2 for x in range(10)]
    # squares is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    

Advanced Use Cases

  1. Multiple Conditions:
    • Example:
      result = [x for x in range(20) if x % 2 == 0 and x % 3 == 0]
      # result is [0, 6, 12, 18]