9

Usually when creating certain sequences of numbers, python and numpy offer some syntactic sugar to do so in a simple way, without generating them yourself with for-loops, e.g. range(start, stop, step).

I'm having a rather simple problem that I'm struggling to solve in an elegant way: Generate a list of the powers of two. E.g. list = [1, 2, 4, 8, ...].

I came up with

n_powers = 4
list = np.zeros(n_powers)
for i in range(0, n_powers): 
    list[i] = 2 ** i

Is there a better way to do this?

3
  • 1
    "Better" how? What shortcomings do you observe in the solution you have now? Commented Mar 9, 2018 at 18:49
  • 2
    list is not a list it is a numpy.ndarray. These are two different data-structures that typically have different idiomatic approaches to doing things. If your list variable were indeed a list object, this would be a perfectly reasonable way of doing it, except you would normally use .append on an empty list, and not pre-allocate it. Commented Mar 9, 2018 at 18:50
  • Also don't use list as a variable name because your overwrite the built-in type list then. Commented Mar 9, 2018 at 18:53

6 Answers 6

19

You seem to be using NumPy, so why not just do this -

>>> 2 ** np.arange(4)
array([1, 2, 4, 8])

This is broadcasted exponentiation.

Sign up to request clarification or add additional context in comments.

4 Comments

Cool, something like this is what I was looking for - my mistake was not using numpy before. I tried: list = range(0,4) and then list = 2 ** list gave me an error TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
@Honeybear Yes, because pure python lists do not support vectorized arithmetic.
2 ** np.array() vs np.array() ** 2 gives different results
well, yes, if you think about it, it makes perfect sense. The first expression raises 2 to the power of the elements in the array, the second expression raises the elements in the array to the power of 2. @Edi
8

Maybe just:

l = [2**i for i in range(n)]

Comments

2
limit = int(input('Limit: '))
l = []

for i in range(limit):
    l.append(2**i)

It is also good to not use list as a variable as is is a datatype and has a built-in list() function

Comments

2

As per the documentation, you can use power() function to increase to desired power in numpy.

i.e. to increase in the power of 2 you can use this: np.power(np.arange(4), 2)

Comments

1

I am not sure what your question is You can just do:

n_powers = 4
list = []
for i in range(n_powers):
    list.append(2 ** i)
print(list)

1 Comment

as @Crawley said, it is better not to use list as a variable but it still works.
0

In the case of this problem (powers of 2) you can use the following Numpy method.

np.exp2(np.arange(4))

Out: array([1., 2., 4., 8.])

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.