Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to initialize a list with a range of numbers in Python

1 Answer

0 votes
# Demonstrating several ways to build a list containing a range of numbers.
# Each function shows a different style that experienced developers commonly use.

# Build a list using the built‑in range() constructor.
# This is the most direct and widely used approach.
def make_range_builtin(start, end):
    # range() produces a lazy sequence; list() materializes it.
    return list(range(start, end))


# Build a list using a list comprehension.
# This is expressive and works well when transforming values.
def make_range_comprehension(start, end):
    # Generates numbers from start to end‑1.
    return [n for n in range(start, end)]


# Build a list using a generator expression and list().
# Useful when you want to keep the expression flexible.
def make_range_generator(start, end):
    return list(n for n in range(start, end))


# Build a list using a manual loop.
# Clear and explicit; helpful when adding logic inside the loop.
def make_range_loop(start, end):
    values = []
    for n in range(start, end):
        values.append(n)
    return values


# Build a list using itertools.count.
# count() is an infinite sequence, so we stop manually.
from itertools import count

def make_range_itertools(start, end):
    values = []
    for n in count(start):
        if n >= end:
            break
        values.append(n)
    return values


# Display a list for demonstration.
def show(label, values):
    print(f"{label}: {values}")


def main():
    # Build lists using all approaches.
    a = make_range_builtin(1, 10)
    b = make_range_comprehension(1, 10)
    c = make_range_generator(1, 10)
    d = make_range_loop(1, 10)
    e = make_range_itertools(1, 10)

    # Show results.
    show("builtin range()", a)
    show("list comprehension", b)
    show("generator expression", c)
    show("manual loop", d)
    show("itertools.count", e)


if __name__ == "__main__":
    main()


"""
run:

builtin range():      [1, 2, 3, 4, 5, 6, 7, 8, 9]
list comprehension:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
generator expression: [1, 2, 3, 4, 5, 6, 7, 8, 9]
manual loop:          [1, 2, 3, 4, 5, 6, 7, 8, 9]
itertools.count:      [1, 2, 3, 4, 5, 6, 7, 8, 9]

"""

 



answered Aug 17 by avibootz
...