1

I want to create a simple list within a while loop in python

I'm using this code

def get_list(input):
    create_cell = []
    for line in input:
        create_cell.append(line)
    return create_cell

x=0
c = [x,'a','b']
while x < 5:
    new_row = get_list(c)
    print (new_row)
    x = x + 1

It gives the following output

[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']

The output what I want is:

[0, 'a', 'b']
[1, 'a', 'b']
[2, 'a', 'b']
[3, 'a', 'b']
[4, 'a', 'b']
Barmar
  • 741,623
  • 53
  • 500
  • 612
Emilio Galarraga
  • 659
  • 1
  • 8
  • 14

1 Answers1

1

Assigning to x doesn't change c. You need to update that as well:

while x < 5:
    new_row = get_list(c)
    print (new_row)
    x = x + 1
    c[0] = x
Barmar
  • 741,623
  • 53
  • 500
  • 612