IndexError: pop from empty list
When popping, list temp_animals shouldn't be empty
number_of_animals=12
animals=["Dog","Cat","Rabbit"]
temp_animals=animals
while (number_of_animals)>0:
temp_animals=animals
print(temp_animals(0)
temp_animals.pop(0)
number_of_animals-=1
It should be setting temp_animals to = ["Dog","Cat","Rabbit"] before its popped. but instead the list animals is emptying itself? The desired output should look something like
'''
Dog
Dog
Dog
Dog
Dog
Dog
Dog
Dog
Dog
Dog
Dog
Dog
'''
CodePudding user response:
In python, list is like an object. Assigning a list to another variable is like a pointer to the original list in the memory (for optimization purposes). You would have to use list.copy()
to create a true copy of the list that doesn't alter the original.
CodePudding user response:
Try using for loop -
number_of_animals=12
animals=["Dog","Cat","Rabbit"]
for _ in range(number_of_animals):
print(animals[0])