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]
π Recommended: List Comprehension in Python β A Helpful Illustrated Guide
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]
π Recommended: List Comprehension in Python β A Helpful Illustrated Guide
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]
π Recommended: Python map()
β Finally Mastering the Python Map Function [+Video]
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]
π Recommended: Python range()
Function β A Helpful Illustrated Guide
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! π