Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Daniel Čupak
Daniel Čupak
6,602 Points

multiply position in a list

I want to append a list hundred times like this:

x = [0, 0, 1]
iters = 0
    while iters < 100:

    x = x + [0,0,3]
    print(x)

    iters += 1

but I would like it everytime to grow the third element by 3 e.g. [0, 0, 1, 0, 0, 3, 0, 0, 9] etc. but I am not able to figure it out...

2 Answers

Hi Daniel

Does this code work for you. See below

mylist = [0, 0, 1]
new_list=[]
for i in range(1,101):
  new_list=[mylist[0],mylist[1],(i*3)]
  mylist.extend(new_list)
print(mylist)


output 

[0, 0, 1, 0, 0, 3, 0, 0, 6, 0, 0, 9, 0, 0, 12, 0, 0, 15, 0, 0, 18, 0, 0, 21, 0, 0, 24, 0, 0, 27, 0, 0, 30, 0, 0, 33, 0, 0, 36, 0, 0, 39, 0, 0, 4
2, 0, 0, 45, 0, 0, 48, 0, 0, 51, 0, 0, 54, 0, 0, 57, 0, 0, 60, 0, 0, 63, 0, 0, 66, 0, 0, 69, 0, 0, 72, 0, 0, 75, 0, 0, 78, 0, 0, 81, 0, 0, 84, 0
, 0, 87, 0, 0, 90, 0, 0, 93, 0, 0, 96, 0, 0, 99, 0, 0, 102, 0, 0, 105, 0, 0, 108, 0, 0, 111, 0, 0, 114, 0, 0, 117, 0, 0, 120, 0, 0, 123, 0, 0, 1
26, 0, 0, 129, 0, 0, 132, 0, 0, 135, 0, 0, 138, 0, 0, 141, 0, 0, 144, 0, 0, 147, 0, 0, 150, 0, 0, 153, 0, 0, 156, 0, 0, 159, 0, 0, 162, 0, 0, 16
5, 0, 0, 168, 0, 0, 171, 0, 0, 174, 0, 0, 177, 0, 0, 180, 0, 0, 183, 0, 0, 186, 0, 0, 189, 0, 0, 192, 0, 0, 195, 0, 0, 198, 0, 0, 201, 0, 0, 204
, 0, 0, 207, 0, 0, 210, 0, 0, 213, 0, 0, 216, 0, 0, 219, 0, 0, 222, 0, 0, 225, 0, 0, 228, 0, 0, 231, 0, 0, 234, 0, 0, 237, 0, 0, 240, 0, 0, 243,
 0, 0, 246, 0, 0, 249, 0, 0, 252, 0, 0, 255, 0, 0, 258, 0, 0, 261, 0, 0, 264, 0, 0, 267, 0, 0, 270, 0, 0, 273, 0, 0, 276, 0, 0, 279, 0, 0, 282, 
0, 0, 285, 0, 0, 288, 0, 0, 291, 0, 0, 294, 0, 0, 297, 0, 0, 300]