Finding Multiples of a Number in Python: A Practical Guide

Whether you are working through a divisibility puzzle, spreading a workload across fixed-size batches, or sampling every n-th record of a dataset, you will regularly need to produce or recognize multiples of a given number. Python makes this nearly trivial once you pick the right tool: range() with a step, a comprehension, a plain loop, or a lazy iterator from itertools.

This guide covers both sides of the problem — generating the multiples of a number in Python and checking whether an existing value is a multiple — then goes further: common multiples of several numbers, custom intervals, negative values, lazy generation for huge ranges, and the mistakes that silently produce wrong results. Every example uses only the standard library and runs on the current stable CPython line (Python 3.15, released in October 2026).

Table of Contents#

  1. What Counts as a Multiple?
  2. The Fast Path: range() with a Step
  3. Five Ways to Generate Multiples of a Number in Python
  4. How to Check Whether a Number Is a Multiple
  5. Common Multiples of Two or More Numbers with math.lcm
  6. Multiples Inside a Custom Interval (and Negative Multiples)
  7. Lazy Multiples: Generator Expressions and itertools
  8. Real-World Use Cases
  9. Performance and Memory Comparison
  10. Common Mistakes and Best Practices
  11. Conclusion
  12. References

What Counts as a Multiple?#

In arithmetic, a multiple of an integer n is any integer m that can be written as m = n * k for some integer k. Multiplying n by 1, 2, 3, ... gives its positive multiples: for n = 4 that is 4, 8, 12, and so on.

Two details matter when this idea moves into code:

  • Zero is special. 0 is a multiple of every integer (n * 0 == 0), while the only multiple of 0 itself is 0 (0 * k == 0 for every k). In practice you almost always mean positive, non-zero multiples, so generation normally starts at k = 1.
  • There are two different jobs. Sometimes you must generate the multiples of a number (all of them up to a limit); other times you must test whether an existing value is a multiple of something. They call for different code, and this guide covers both.

The Fast Path: range() with a Step#

Before the long tour, here is the idiomatic answer. Python's built-in range(start, stop, step) emits values start + step * i for i = 0, 1, 2, ... while the value is below stop (for a positive step). Setting start and step to n produces exactly the multiples of n:

# All multiples of 6 up to 48 (inclusive)
multiples = list(range(6, 49, 6))
print(multiples)
# [6, 12, 18, 24, 30, 36, 42, 48]

The stop bound is exclusive, so passing limit + 1 includes the limit itself when it is a multiple. Two properties make range() the default choice:

  • It holds only start, stop, and step, so its memory use is the same whether it represents ten values or ten billion (see the range documentation).
  • Its arguments must be integers (anything implementing __index__); a float raises TypeError, and a step of 0 raises ValueError.

Keep that one-liner in mind. The rest of the guide explains when you need something else.

Five Ways to Generate Multiples of a Number in Python#

1. range() with a step — the default choice#

Wrap the one-liner in a function so the boundary conditions are handled once:

def multiples_up_to(n, limit):
    if n == 0:
        raise ValueError("n must be non-zero")
    n = abs(n)  # accept a negative n without breaking the step
    return list(range(n, limit + 1, n))
 
print(multiples_up_to(7, 40))
# [7, 14, 21, 28, 35]

Use this whenever the output should be "every n-th integer up to a bound" — it is fast, readable, and hard to get wrong. Iterate over the range object directly (for m in range(n, limit + 1, n)) when you do not need a list in memory.

2. List comprehension — build from the multipliers#

Sometimes the multiplier k is what your logic is really about (say, you also need k itself, or you apply extra conditions). A comprehension makes that explicit:

n, limit = 6, 48
multiples = [n * k for k in range(1, limit // n + 1)]
print(multiples)
# [6, 12, 18, 24, 30, 36, 42, 48]

limit // n is the largest multiplier that keeps n * k <= limit, which is what bounds the loop. Comprehensions also make filtering trivial — for example, the multiples of 6 that are also greater than 20: [n * k for k in range(1, limit // n + 1) if n * k > 20].

3. for loop — explicit control#

When the computation per multiple is more than a single expression — accumulating totals, sending requests, writing rows — the straightforward loop is the clearest option:

def multiples_accumulated(n, limit):
    if n == 0:
        raise ValueError("n must be non-zero")
    n = abs(n)
    results = []
    for k in range(1, limit // n + 1):
        results.append(n * k)  # do real work with each multiple here
    return results
 
print(multiples_accumulated(7, 28))
# [7, 14, 21, 28]

4. while loop — stop conditions that change#

A while loop wins when the stopping rule is not a simple bound: the limit may move, each step may depend on I/O, or the multiplier may grow non-linearly.

def multiples_while(n, limit):
    if n == 0:
        raise ValueError("n must be non-zero")
    n = abs(n)
    results, k = [], 1
    while n * k <= limit:
        results.append(n * k)
        k += 1
    return results
 
print(multiples_while(5, 27))
# [5, 10, 15, 20, 25]

5. map() with lambda — functional pipelines#

If your codebase chains map(), filter(), and friends, generating multiples as a transformation of range() fits the style:

n, limit = 8, 30
multiples = list(map(lambda k: n * k, range(1, limit // n + 1)))
print(multiples)
# [8, 16, 24]

This is functionally equivalent to the comprehension in method 2. Prefer the comprehension unless you are already composing a pipeline — most Python teams find it easier to read.

How to Check Whether a Number Is a Multiple#

Generating multiples is only half the story. To test whether value is a multiple of n, use the modulo operator: the remainder of value % n is zero exactly when n divides value evenly.

def is_multiple(value, n):
    if n == 0:
        raise ValueError("n must be non-zero")
    return value % n == 0
 
print(is_multiple(21, 7))   # True  -> 21 is a multiple of 7
print(is_multiple(-21, 7))  # True  -> signs do not matter
print(is_multiple(22, 7))   # False -> 22 = 7*3 + 1

Notes:

  • Python's % returns 0 for exact division of negative values too, so -21 % 7 == 0 — you do not need abs().
  • value % 0 raises ZeroDivisionError, which is why the guard above converts it into a clearer ValueError.
  • Filter collections in one line: candidates = [x for x in data if is_multiple(x, 25)].

Common Multiples of Two or More Numbers with math.lcm#

A number is a common multiple of a and b precisely when it is a multiple of their least common multiple (LCM). That observation turns "common multiples of two numbers" into the same range() problem you already know, and the standard library computes the LCM for you with math.lcm() (available since Python 3.9), which accepts any number of integer arguments.

import math
 
def common_multiples(a, b, limit):
    step = math.lcm(a, b)           # smallest positive common multiple
    return list(range(step, limit + 1, step))
 
print(math.lcm(4, 6))               # 12
print(common_multiples(4, 6, 60))
# [12, 24, 36, 48, 60]

Compare this with the naive approach of materializing both multiples lists and intersecting them: that costs memory proportional to the limit and two full list builds, while the LCM approach stores one integer and a range object. A few semantics worth remembering from the docs:

  • If every argument is non-zero, math.lcm() returns the smallest positive integer divisible by all of them, and it accepts more than two arguments: math.lcm(3, 4, 5) is 60.
  • If any argument is zero, the result is 0.
  • Called without arguments, it returns 1.
  • Results are always non-negative, so math.lcm(-3, 4) is 12.

Multiples Inside a Custom Interval (and Negative Multiples)#

Often the question is narrower: "which multiples of 6 lie between 20 and 50?" The trick is to start the range at the first multiple at or after the lower bound. Ceiling division gives it — for a positive step, first = -(-start // n) * n:

def multiples_between(n, start, end):
    if n == 0:
        raise ValueError("n must be non-zero")
    step = abs(n)
    first = -(-start // step) * step   # smallest multiple of step >= start
    return list(range(first, end + 1, step))
 
print(multiples_between(6, 20, 50))
# [24, 30, 36, 42, 48]
print(multiples_between(6, -25, 5))
# [-24, -18, -12, -6, 0]

Note that 0 shows up in the second result because 0 is a multiple of 6 — add if m != 0 if you want strictly non-zero multiples.

For negative multiples of a negative number, give range() a negative step. Remember the bound rule flips: with a negative step, values must stay above stop, so use limit - 1 to include a negative limit.

print(list(range(-3, -22, -3)))
# [-3, -6, -9, -12, -15, -18, -21]

Lazy Multiples: Generator Expressions and itertools#

Converting a large range to a list defeats much of its value. Two standard patterns generate multiples one at a time, so memory stays constant regardless of how many you consume.

Generator expression — same math, no list:

n = 3
stream = (n * k for k in range(1, 20 // n + 1))
print(next(stream), next(stream), next(stream))
# 3 6 9

Infinite streams with itertools — itertools.count(start, step) never ends; combine it with itertools.islice to take as many multiples as you need:

import itertools
 
multiples_of_9 = itertools.count(9, 9)   # 9, 18, 27, ...
print(list(itertools.islice(multiples_of_9, 8)))
# [9, 18, 27, 36, 45, 54, 63, 72]

This pattern is ideal for "keep producing multiples until some other condition stops me" — you can loop over itertools.count(n, n) and break whenever your real stopping rule triggers, never building a list at all.

Real-World Use Cases#

Finding multiples of a number in Python shows up far beyond textbook exercises:

  • Batching and pagination. Split work into chunks of batch_size with index arithmetic over range: [items[i:i + batch_size] for i in range(0, len(items), batch_size)] produces [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j']] for ten items and batch_size = 4. On Python 3.12+, itertools.batched() does this directly (its strict=True flag rejects a short final batch).
  • Sampling every n-th record. Take every fifth element of a sequence via its index: [data[i] for i in range(0, len(data), 5)]. For data = [1, 2, ..., 20] this yields [1, 6, 11, 16] — a quick deterministic sample without the random module.
  • Periodic schedules and timestamps. Every 15-minute slot in a day is range(0, 24 * 60, 15); the same start/stop/step logic drives polling intervals and expiry checks.
  • Filtering by divisibility. Keep records whose identifier is a multiple of 100 (id % 100 == 0), or the values divisible by both 3 and 5 in 1..30, which are exactly [15, 30] — the multiples of math.lcm(3, 5).
  • Coding challenges. Classic puzzles such as "sum all multiples of 3 or 5 below 1000" reduce to a filtered sum: sum(x for x in range(1000) if x % 3 == 0 or x % 5 == 0), which evaluates to 233168.

Performance and Memory Comparison#

All of these approaches run in linear time relative to the number of multiples produced; the real differences are memory and flexibility.

ApproachMemoryBest for
range(n, limit + 1, n)Constant — stores start, stop, step onlyDefault; large limits; simple loops
List comprehensionLinear in the number of multiplesSmall/medium results, filtering
Explicit for loopLinear (whatever you accumulate)Side effects, multi-step work
while loopLinear (whatever you accumulate)Dynamic or changing stop conditions
map() + lambdaLinear once wrapped in list()Functional pipelines
Generator expression / itertools.countConstantUnbounded or streamed results
Set intersection of two multiples listsLinear in limit (two lists + a set)Avoid: prefer math.lcm()

Key takeaways: reach for range() first; materialize a list only when you need random access or repeated iteration; and never build two huge sets to find common multiples when math.lcm() answers in constant memory.

Common Mistakes and Best Practices#

  1. Off-by-one on the upper bound. range() excludes stop, so range(5, 30, 5) stops at 25 and silently drops 30. Use limit + 1 to include the limit.
  2. Forgetting that zero is a multiple. Generation starting at k = 0 includes 0; generation starting at range(n, ..., n) or k = 1 does not. Decide deliberately which one your caller expects.
  3. Ignoring n = 0. Division-based helpers (limit // n, value % n) raise ZeroDivisionError, and "all multiples of 0" is not a useful sequence anyway. Validate inputs up front with raise ValueError("n must be non-zero").
  4. Negative n breaking the limit // n pattern. limit // n with a negative n yields a non-positive bound and an empty result. Normalize first (n = abs(n)) or generate negatives explicitly with a negative step.
  5. Passing floats to range(). range(0, 10, 2.5) raises TypeError: 'float' object cannot be interpreted as an integer. Convert to int first, or scale the problem into integers (e.g., work in tenths).
  6. Materializing gigantic lists. Turning range(0, 10**9, 7) into a list can exhaust memory. Iterate the range object or use a generator expression instead — a range object uses the same small amount of memory at any size.
  7. Re-deriving the LCM by hand. math.lcm() (Python 3.9+) is tested, fast, and accepts several arguments; itertools.batched() (Python 3.12+) similarly replaces hand-rolled chunking. Prefer the standard library when it already has the tool.

Conclusion#

Finding multiples of a number in Python is a one-liner at its core: range(n, limit + 1, n) generates them, and value % n == 0 recognizes them. The craft lies in the edges — inclusive bounds, zero and negative inputs, lazy generation instead of giant lists, and using math.lcm() for common multiples. Choose range() as your default, reach for comprehensions when you also need filtering, use loops when the work per multiple is substantial, and stream with itertools when the sequence is large or unbounded. With those rules in mind, the same three-line pattern scales from a homework puzzle to a production batching job.

References#