0

I am trying to initialize a numpy array or a list with ascending values. The number of values is n.

e.g: If n = 10

[0,2,4,6,8,10,12,14,16,18]

If n = 2

[0,2]

I am aware that I could use a for like this:

result = []
for x in range(n):
   result.append(x*2)

But when n gets larger this would take a while so I was looking for a faster way.

1
  • 3
    try np.arange() Commented Jul 29, 2020 at 14:46

2 Answers 2

7

There is numpy.arange which supports a step parameter:

result = np.arange(0, 2*n, 2)
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively, for a list you can use range's step parameter:

result = list(range(0, 2*n, 2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.