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
🌐
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 ...
🌐
DataCamp
campus.datacamp.com › courses › introduction-to-python-for-finance › arrays-in-python
Generating a sequence of numbers | Python
The arguments for arange() include the start, stop, and step interval as shown below: ... Create an array company_ids containing the numbers 1 through 7 (inclusive).
🌐
Howchoo
howchoo.com › python › python-range-function
Use the Python range() Function to Generate Sequences of ...
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
🌐
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.
🌐
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) ...
🌐
Patrickwalls
patrickwalls.github.io › mathematicalpython › python › sequences
Sequences - Mathematical Python
It is very inefficient to create ... 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 ......
Find elsewhere
🌐
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
🌐
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.
🌐
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
🌐
Stack Overflow
stackoverflow.com › questions › 34287404 › how-to-generate-sequence-number-in-python
json - How to generate sequence number in python? - Stack Overflow
db.sequences.find() > { "_id": "alerts", "last_value": 234 } Then you can use findAndModify to generate a new value and return it in the same operation (no race conditions, safe and reliable). One downside of this approach is that you exert more load on your database to satisfy this piece of business logic. Depending on your traffic, you might need more powerful hardware, compared to using built-in object ids.
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.

🌐
Quora
quora.com › How-do-you-create-a-sequence-in-Python
How to create a sequence in Python - Quora
Answer (1 of 4): In Python a list can be created like this: [code]my_list = [] [/code]or like this: [code]my_list = list() [/code]or with items of any data type: [code]my_list = ["apple", 1, 3.01] [/code]An sequence of number can be generated by the built-in function range(start, stop, step). ...
🌐
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
🌐
YouTube
youtube.com › suchismita adhikary
How to create a sequence of numbers in Python - YouTube
List ConstructorRange function#suchi#python#list#range
Published: May 11, 2022
Views: 375
🌐
YouTube
youtube.com › blogize
Generating a Sequence of Numbers in Python - YouTube
Summary: Learn how to generate and print sequences of numbers in Python, including the usage of for loops, the range function, and list comprehensions.---Gen...
Published: September 4, 2024
Views: 23
🌐
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 you ask for it. This also means generators can represent infinite sequences. For example, the following generator represents all square numbers...