Array of 2^n

Glen Wheeler wheelege at tsn.cc
Sun Oct 21 03:13:04 EDT 2001


> Okay, python newbie here. I'm liking what I see, but it seems like there
> are holes in the features of python's objects. I remember programming in
> squeak (an extension of smalltalk), and you could do complex operations on
> lists, and it would apply the result to each item. I want to make a n-item
> list such that mylist[i] = 2**i. How can I generate such a list in Python?

  There are two standard ways...

>>> i = 10
>>> l = map(lambda x:2**x, range(i))
>>> l
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

  And...

>>> l = []
>>> for j in range(i):
...  l.append(2**j)
...
>>> l
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

  HTH,
  Glen





More information about the Python-list mailing list