Every number from 1,2,5,6,9,10... is divisible by 4 with remainder 1 or 2.

>>> ','.join(str(i) for i in xrange(100) if i % 4 in (1,2))
'1,2,5,6,9,10,13,14,...'
Answer from Aleksei astynax Pirogov on Stack Overflow
🌐
Vultr Docs
docs.vultr.com › python › built-in › range()
Python range() - Generate Number Sequence
September 27, 2024 - In this article, you will learn how to effectively use the range() function in Python. Discover how to utilize this function to create sequences of numbers, understand its parameters, and see how it can be applied in different programming contexts like loops and list comprehensions.
🌐
YouTube
youtube.com › learning software
create sequence of numbers in python with 'range' function - YouTube
In this video we will learn how to use dictionaries and some essential/useful skills while using them in pythonBlog post for this video - https://nagasudhir....
Published: August 10, 2020
Views: 1K
🌐
DataCamp
campus.datacamp.com › courses › introduction-to-python-for-finance › arrays-in-python
Generating a sequence of numbers | Python
You may want to create an array of a range of numbers (e.g., 1 to 10) without having to type in every single number. The NumPy function arange() is an efficient way to create numeric arrays of a range of numbers.
🌐
GitConnected
levelup.gitconnected.com › the-fastest-way-to-generate-a-sequence-in-python-a61da7f87852
The fastest way to generate a sequence in Python | by Astronomy not Astrology, hunty... | Level Up Coding
May 14, 2020 - The built-in method range([start, ]stop, [step]) was my first introduction to generating sequences in Python. The optional arguments in the function are shown in square brackets. The range() method generates an immutable object that is a sequence of numbers.
🌐
TutorialsPoint
tutorialspoint.com › How-to-generate-sequences-in-Python
How to generate sequences in Python?
February 27, 2025 - Following are the various techniques to generate sequences in Python ? ... We can generate a sequence using a loop by starting with an empty sequence and appending values that meet a specified condition. In the following example, we have generated a sequence of all even numbers below 20 using ...
🌐
Linux Hint
linuxhint.com › python-generate-sequence-of-numbers
Linux Hint – Linux Hint
July 24, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Find elsewhere
🌐
Patrickwalls
patrickwalls.github.io › mathematicalpython › python › sequences
Sequences - Mathematical Python
For example, simply typing out the numbers from 1 to 20 takes a long time! numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] print(numbers) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Python has a beautiful syntax for creating lists called list ...
Top answer
1 of 4
4

If the closed form is available you can use a list comprehension. This has the advantage that all the required memory can be allocated right at the beginning:

seq = [(i + 1)**2 for i in range(1, n+1)]

Repeatedly appending to a list causes internal resizing of the underlying memory and thus involves unnecessary memory allocations and copies.

Without a closed form you can still use a generator:

def gen_seq(n):
    a = 0
    for i in range(1, n+1):
        a += 2*i - 1
        yield a

Performance comparison

In [1]: def f1(n): 
   ...:     return [(i + 1)**2 for i in range(1, n+1)] 
   ...:                                                                                       

In [2]: def gen_seq(n): 
   ...:     a = 0 
   ...:     for i in range(1, n+1): 
   ...:         a += 2*i - 1 
   ...:         yield a 
   ...:                                                                                       

In [3]: def f2(n): 
   ...:     return list(gen_seq(n)) 
   ...:                                                                                       

In [4]: %timeit f1(100_000)                                                                   
29.7 ms ± 271 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [5]: %timeit f2(100_000)                                                                   
16.1 ms ± 176 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

The generator version is almost 2x faster than the list comprehension. This is because the recursive version benefits from the relatively simple operations that are involved. Multiplying an integer by 2 is simply a 1-lshift and adding or subtracting a number is an O(N) operation where N is the number of digits. Multiplying two integers however is O(N*log(N)) and hence takes more time to compute. The recursive version benefits from the already-computed part a_{n-1} which it can reuse at each step.

2 of 4
1

Specifically in this case, you can just use list(range(1, n)) which is much faster

import time
t = time.time()
print(list(range(1, 1_000_000)))
print(time.time() - t)

Takes less than 0.5 seconds.

🌐
Camdenreslink
code.camdenreslink.com › dev › 7-ways-to-create-sequences-in-python
7 Ways to Create Sequences in Python — Camden Reslink
December 5, 2018 - A generator function is a special function in Python, that can yield multiple values, instead of just a single return. Calling the generator creates an iterator, which can be iterated through with a for-loop. Generators are computed lazily. That means the next value isn’t calculated until ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-create-list-of-numbers-with-given-range
Create List of Numbers with Given Range - Python - GeeksforGeeks
List comprehension is a concise and efficient way to create lists by iterating over an iterable like range(), in a single line. It simplifies code by eliminating the need for explicit for loops and append() calls.
Published: July 11, 2025
🌐
CodeSpeedy
codespeedy.com › home › sequence generator in python
Sequence generator in Python - CodeSpeedy
February 9, 2020 - #A NORMAL FUNCTION T GET CUBES def cube(n): result = [] for x in range(n): result.append(x**3) return result print(cube(8)) #THIS WILL MAKE A LIST FOR OUTPUT WHICH WILL TAKE A LOT OF MEMORY #INSTEAD OF THIS WE CAN USE FOR LOOP TO PRINT ONE ELEMENT AT A TIME for x in cube(10): print(x) #TO MAKE THIS CODE SHORTER WE CAN USE YIELD IN THE FUNCTION def cube(n): for x in range(n): yield x**3 for x in cube(10): print(x) ...
🌐
Howchoo
howchoo.com › python › python-range-function
Use the Python range() Function to Generate Sequences of ...
Not sure what version of Python you’re running? Time to find out! How To Create Boxplots, Scatterplots, and Histograms in Python Using Matplotlib · Python is a very popular programming language for data visualization. ... In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
🌐
Blogger
nagasudhir.blogspot.com › 2020 › 07 › create-sequence-of-numbers-in-python.html
Create a sequence of numbers in python
July 4, 2021 - Use linspace function for creating a sequence with fixed number of samples from start to end The syntax is np.linspace(start, stop, num) # import the numpy module import numpy as np # get 15 evenly spaced numbers from 1 to 8 using linspace function ...
🌐
Machinelearninghelp
machinelearninghelp.org › programming-for-machine-learning › how-to-add-a-sequence-of-numbers-in-python
Add a Sequence of Numbers in Python
Here is a step-by-step guide to generating an arithmetic progression using Python: def generate_arithmetic_progression(start, end, step): """ Generate an arithmetic progression from start to end with the given step. Args: start (int): The starting value of the sequence.
🌐
University of Washington
sites.math.washington.edu › ~conroy › sequenceNoise › pythonCodeExamples.htm
Python code examples for generating sequences
Here is some Python 2 code which generates the sequence of all mulitples of 7 (A008589). import math for n in range(1,1000000): if ((n%7)==0): print n Here is some Python 2 code which generates the sequence of integers whose digits sum is 12 ( A235151).
🌐
Medium
medium.com › @nagasudhirpulla › create-sequences-with-range-function-in-python-6b98a0fbbf8b
Create sequences with range function in python | by Naga Sudhir | Medium
August 21, 2021 - # create sequence from 2 to 12 with steps of 2, i.e., 2,4,6,8,10,12 x = range(2,13,2)# iterate over the sequence using for loop and in operator for n in x: print(n) # this code should print 2,4,6,8,10,12 in each line of the console