I need to write a function that generates a list of n odd numbers, starting at 1. If input is 12, output should be [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]
. If input is 10
, output should be [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Asked
Active
Viewed 6,145 times
1

Georgy
- 12,464
- 7
- 65
- 73

Jason Walker
- 23
- 1
- 3
-
Do you know about the [modulo operator](https://stackoverflow.com/questions/4432208/what-is-the-result-of-in-python)? – ForceBru Jul 13 '19 at 10:17
-
Hint: what will be the last item of a list of *n* odd elements? – Willem Van Onsem Jul 13 '19 at 10:17
-
Possible duplicate of [python - checking odd/even numbers and changing outputs on number size](https://stackoverflow.com/questions/13636640/python-checking-odd-even-numbers-and-changing-outputs-on-number-size) – Nouman Jul 13 '19 at 10:25
-
_I don't even know if I'm going to right direction_ To start, does the code work, or not? – AMC Aug 25 '20 at 21:26
10 Answers
2
Almost..
def odd(n):
nums = []
for i in range(1, 2*n, 2):
nums.append(i)
return nums
we know that every other number is odd, so we have to "count" up to 2*n
to include all of them. The range
function takes a third argument that indicates how many elements to skip in each iteration.

thebjorn
- 26,297
- 11
- 96
- 138
2
def odd(n):
return list(range(1, 2*n, 2))
print(odd(10))
print(odd(12))
The output:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]

bart
- 1,003
- 11
- 24
1
Code with list comprehenssion
:-
def odd(n):
return [num for num in range(1, n*2+1,2)]
odd(10)
Output
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
0
range(..)
can take a third parameter that is the step
the amount the value increments each iteration. The last item of a list with the first n odd elements, is 2×n-1, so we can write this as:
def odd(n):
return list(range(1, 2*n, 2))

Willem Van Onsem
- 443,496
- 30
- 428
- 555
0
Or with numpy
import numpy as np
def odd(n):
return np.arange(1, 2*n, 2)
odd(10) -> array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
odd(12) -> array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23])

L3n95
- 1,505
- 3
- 25
- 49
0
Documentation: https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.arange.html
import numpy as np
def odd(n):
return np.arange(1,n*2,2)
-
1Code only answers can be improved by adding some explanation to it. Please take some time to explain what your code does. – DaFois Jul 13 '19 at 11:00
0
You can use this also:-
def odd(n):
nums = []
for i in range(1, n*2+1):
if i%2==0:
pass
else:
nums.append(i)
return nums
odd(10)
Output
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
-
Welcome to SO. When answering a question, it’s always great to add some explanation so the OP can learn something from your code. – trotta Jul 13 '19 at 12:00
-
1
0
def f(n):
return[i for i in range(1,n*2,2)]
or
def E(n):
list=[]
list.extend(i for i in range(1,2*n,2))
return(list)

Muhammad Nour
- 1
- 2
-
1Welcome to StackOverflow! Please take the [tour] and read [answer]. While code-only answers may answer the question and fix OP's problem, adding an explanation of _why_ it works will significantly improve your answers. Also, if you're reanimating such an old, inactive question, your answer needs to add something significant to what's already been said by the other answers. That isn't the case here because your answer is essentially a verbatim copy of the others. – Pranav Hosangadi Aug 25 '20 at 21:02
-1
With lambda
AND numpy
. With the help of lambda
you can write function in one line AND numpy.arange
is like range()
but it returns an array:-
import numpy as np # Importing numpy module
odd = lambda num: np.arange(1,num*2+1,2)
odd(10)