Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python list comprehension

List comprehensions in Python provide a concise way to create a new list by applying an expression to each item in an existing list or other iterable. They can be used to create lists in a more readable and efficient way compared to using for loops or the map() function. This tutorial will show you how to use list comprehensions in Python.

Basic list comprehension syntax:

The general syntax for a list comprehension is:

new_list = [expression for item in iterable if condition]
  • expression: The operation you want to apply to each item in the iterable.
  • item: A variable used to represent each item in the iterable.
  • iterable: An iterable object (e.g., list, tuple, range, or string) whose items you want to process.
  • condition (optional): A filter that selects items from the iterable based on a condition.

Examples of list comprehensions:

  • Create a list of squares of the numbers from 1 to 10:
squares = [x**2 for x in range(1, 11)]
print(squares)  # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  • Create a list of even numbers from 1 to 20:
even_numbers = [x for x in range(1, 21) if x % 2 == 0]
print(even_numbers)  # Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
  • Convert a list of temperatures in Celsius to Fahrenheit:
celsius = [0, 10, 20, 30, 40, 50]
fahrenheit = [(9/5)*temp + 32 for temp in celsius]
print(fahrenheit)  # Output: [32.0, 50.0, 68.0, 86.0, 104.0, 122.0]
  • Create a list of lowercase words from a list of mixed-case words:
words = ['Apple', 'BaNaNa', 'Cherry', 'ORANGE']
lowercase_words = [word.lower() for word in words]
print(lowercase_words)  # Output: ['apple', 'banana', 'cherry', 'orange']

Nested list comprehensions:

You can use nested list comprehensions to create a list of lists or to flatten a list of lists into a single list.

  • Create a list of lists representing a multiplication table:
multiplication_table = [[i * j for j in range(1, 11)] for i in range(1, 11)]
for row in multiplication_table:
    print(row)
  • Flatten a list of lists into a single list:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat_list = [item for sublist in nested_list for item in sublist]
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In the example above, the first for loop iterates over the sublists, and the second for loop iterates over the items within each sublist.

  1. How to Use List Comprehensions in Python:

    • List comprehensions provide a concise way to create lists.
    # Example
    squares = [x**2 for x in range(1, 6)]
    
  2. Creating Lists with Comprehension in Python:

    • List comprehensions create lists using a compact and readable syntax.
    # Example
    even_numbers = [x for x in range(10) if x % 2 == 0]
    
  3. Filtering and Transforming Data with List Comprehension:

    • Apply conditions to filter or transform data during list creation.
    # Example
    squared_even_numbers = [x**2 for x in range(10) if x % 2 == 0]
    
  4. Advanced Techniques for List Comprehension in Python:

    • Use expressions and functions for more complex transformations.
    # Example
    mixed_data = [str(x) if x % 2 == 0 else x for x in range(5)]
    
  5. Nested List Comprehensions in Python:

    • List comprehensions can be nested to create more complex structures.
    # Example
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    flattened_list = [num for row in matrix for num in row]
    
  6. List Comprehension vs Traditional For Loop in Python:

    • List comprehensions provide a more concise and expressive way to create lists compared to traditional for loops.
    # Example with for loop
    squares = []
    for x in range(1, 6):
        squares.append(x**2)
    
    # Example with list comprehension
    squares = [x**2 for x in range(1, 6)]