# 11 Ways to Create a List of Even Numbers in Python

4/5 - (1 vote)

## Method 1: Using List Comprehension

To create a list of even numbers in Python, you can use the list comprehension `[x for x in range(10) if x%2==0]` that iterates over all values `x` between 0 and 10 (exclusive) and uses the modulo operator `x%2` to check if `x` is divisible by 2 without remainder, i.e., `x%2==0`.

Here’s a minimal example:

```even_numbers = [x for x in range(10) if x%2==0]
print(even_numbers)
# [0, 2, 4, 6, 8]```

## Method 2: Using a For-loop and Append Method

This method involves a standard `for`-loop and the list’s `append()` method. We iterate from 1 to 10, multiply each number by 2 to get even numbers, and append these to the list.

Here’s an example:

```even_numbers = []
for i in range(1, 11):
even_numbers.append(i * 2)
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Python List `append()` Method

## Method 3: Using a For-loop and List’s += Operator

This method is similar to the first one, but instead of using the `append()` method, we use the list’s `+=` operator to add new elements.

Here’s an example:

```even_numbers = []
for i in range(1, 11):
even_numbers += [i * 2]
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Python In-Place Addition Operator

## Method 4: Using a While-loop

This method uses a `while`-loop to generate the list of even numbers. We start with `i=1` and increment `i` in each iteration until it exceeds 10.

Here’s an example:

```even_numbers = []
i = 1
while i <= 10:
even_numbers.append(i * 2)
i += 1
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Python Loops

## Method 5: Using List Comprehension (✅ One-Liner)

List comprehension is a concise way to create lists in Python. It can be used to generate a list of even numbers by multiplying each number in a range by 2.

Here’s an example:

```even_numbers = [i * 2 for i in range(1, 11)]
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

## Method 6: Using the Map Function (✅ One-Liner)

The `map()` function applies a given function to each item of an iterable (e.g., list) and returns a list of the results. We can use it with a lambda function to generate even numbers.

Here’s an example:

```even_numbers = list(map(lambda x: x * 2, range(1, 11)))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

## Method 7: Using the Filter Function (✅ One-Liner)

The `filter()` function constructs a list from elements of an iterable for which a function returns `True`. Here, we use it with a lambda function to filter even numbers from a range.

Here’s an example:

```even_numbers = list(filter(lambda x: x % 2 == 0, range(1, 21)))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Python `filter()`

## Method 8: Using the Range Function with a Step Argument (✅ One-Liner)

The `range()` function can accept a step argument to skip numbers. By starting at 2 and stepping by 2, we can generate a list of even numbers.

Here’s an example:

```even_numbers = list(range(2, 21, 2))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

## Method 9: Using the Itertools Module (✅ One-Liner)

The `itertools` module is part of the Python standard library and contains many functions that are useful for efficient iteration. Here, we use `itertools.count` to create an infinite sequence of even numbers, and `itertools.islice` to take the first 10.

Here’s an example:

```import itertools
even_numbers = list(itertools.islice(itertools.count(2, 2), 10))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Iterators, Iterables and Itertools

## Method 10: Using Numpy Array (✅ One-Liner)

Numpy is a powerful library for numerical computations in Python. We can use the numpy function `arange()` to generate a sequence of even numbers.

Here’s an example:

```import numpy as np
even_numbers = list(np.arange(2, 21, 2))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: NumPy `arange()`: A Simple Illustrated Guide

## Method 11: Using Generator Expression (✅ One-Liner)

A generator expression is a high-performance, memory–efficient generalization of list comprehensions and generators. In this method, data is not stored in memory all at once but is generated on the fly.

Here’s an example:

```even_numbers = list(i * 2 for i in range(1, 11))
# Output: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

🔗 Recommended: Python Generator Expressions

Also, you may want to check out our free Finxter email academy — join 150k coders worldwide improving their skills together! 👇