Create a List Using List Comprehension in Python

In Python, list comprehension is a concise way to create lists using a single line of code. It follows the syntax:

[expression for item in iterable if condition] 

And is widely used for transforming or filtering sequences efficiently.


Examples

1. Creating a List of Squares Using List Comprehension

We can generate a list of squares of numbers from 1 to 10 using list comprehension.

</>
Copy
# Creating a list of squares using list comprehension
squares = [x**2 for x in range(1, 11)]

# Printing the generated list
print("List of Squares:", squares)

Explanation:

  • The expression x**2 calculates the square of each number.
  • The for loop iterates over numbers from 1 to 10 using range(1, 11).
  • The final list contains squared values of these numbers.

Output:

List of Squares: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

2. Filtering Even Numbers from a List

We can use list comprehension with a condition to filter only even numbers from a range.

</>
Copy
# Creating a list of even numbers using list comprehension
even_numbers = [x for x in range(1, 21) if x % 2 == 0]

# Printing the generated list
print("Even Numbers:", even_numbers)

Explanation:

  • The expression x represents the current number being evaluated.
  • The for loop iterates over numbers from 1 to 20.
  • The condition if x % 2 == 0 filters only even numbers.

Output:

Even Numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

3. Creating a List of First Letters from Words

We can extract the first letter of each word from a list using list comprehension.

</>
Copy
# List of words
words = ["apple", "banana", "cherry", "date"]

# Extracting first letters using list comprehension
first_letters = [word[0] for word in words]

# Printing the generated list
print("First Letters:", first_letters)

Explanation:

  • The expression word[0] extracts the first character of each word.
  • The for loop iterates over the words in the list words.
  • The final list contains the first letter of each word.

Output:

First Letters: ['a', 'b', 'c', 'd']

4. Generating a List of Tuples

We can create a list of tuples where each tuple contains a number and its square.

</>
Copy
# Creating a list of (number, square) tuples
number_tuples = [(x, x**2) for x in range(1, 6)]

# Printing the generated list
print("Number Tuples:", number_tuples)

Explanation:

  • The expression (x, x**2) creates a tuple with a number and its square.
  • The for loop iterates over numbers from 1 to 5.
  • The final list contains tuples where each element represents (number, square).

Output:

Number Tuples: [(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]

Conclusion

List comprehension in Python provides an efficient way to create lists with minimal syntax. The key takeaways include:

  1. Basic Syntax: [expression for item in iterable]
  2. Filtering: Add conditions using if statements.
  3. Transformation: Apply expressions to modify elements.
  4. Tuple Creation: Use parentheses to create lists of tuples.