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']
0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. That was the problem, now it's fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.