Skip to content Skip to sidebar Skip to footer

Using A Loop To Add Objects To A List(python)

I'm trying to use a while loop to add objects to a list. Here's basically what I want to do: class x: pass choice = raw_input(pick what you want to do) while(choice!=0):

Solution 1:

The problem appears to be that you are reinitializing the list to an empty list in each iteration:

while choice != 0:
    ...
    a = []
    a.append(s)

Try moving the initialization above the loop so that it is executed only once.

a = []
while choice != 0:
    ...
    a.append(s)

Solution 2:

Auto-incrementing the index in a loop:

myArr[(len(myArr)+1)]={"key":"val"}

Post a Comment for "Using A Loop To Add Objects To A List(python)"