-2

I am trying to use a for loop to empty out all the contents of animals into pets. There might be a concise code but I want to understand where am I going wrong in the 3 codes below.

1st Code

animals = ['cat','dog','donkey','mouse','pig']
pets=[]
print(animals)
print(pets)
for value in animals:
    animals.pop()=value
    pets=pets.append(value)
print(animals)
print(pets)

2nd Code

animals = ['cat','dog','donkey','mouse','pig']
pets=[]
print(animals)
print(pets)
for value in animals:
    value=aniamls.pop()
    pets=pets.append(value)
print(animals)
print(pets)

3rd Code

animals = ['cat','dog','donkey','mouse','pig']
pets=[]
print(animals)
print(pets)
for value in animals:
    value=aniamls.pop()
    pets.append(value)
print(animals)
print(pets)

Why is the 3rd code at least running, but not the 1st and 2nd codes?

Why are not all the contents of animals appended to pets in the 3rd code?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sai137
  • 1
  • What makes you think that the first two aren’t running? – quamrana Aug 21 '21 at 19:41
  • 1
    `pets = animals.copy()` should be enough – Alex Aug 21 '21 at 19:42
  • 3
    `animals.pop()=value` isn't syntactically valid code. You can't have function calls on the right of `=`. And for you third code, see [here](https://stackoverflow.com/questions/742371/why-does-python-skip-elements-when-i-modify-a-list-while-iterating-over-it) for why elements are skipped. – Carcigenicate Aug 21 '21 at 19:42
  • Welcome. Did you try to run the third code snippet? There is a typo - trying to access `aniamls` when `animals` is defined instead should not work. Thanks for your time providing code that runs somehow - the third snippet should give a NameError and bail out ... – Dilettant Aug 21 '21 at 19:53

2 Answers2

0

So to answer your first question. The reason the 3rd code is passing and the first 2 are not, is because

pets.append(value)

Is the correct way to append to the pets array.

Not all the contents of pets are appended to the animals is because pop removes the last item of the index by default if you don't write an index. If you enter an index as you go through your loop, you will be able to catch the missing ones

goudarziha
  • 327
  • 5
  • 20
0

Answer to first question:

In the first piece of code, Python raises a SyntaxError because of this line: animals.pop()=value. In Python, you assign a variable like this: x = 3, where the variable name is on the left of the equals sign and the value you want to assign the variable to is on the right of the equals sign. animals.pop() is a function that returns the last item in the animals list, so it cannot be a variable name. You've simply swapped the 2 sides of the equals sign. To make Python not throw an error you should replace that line with value = animals.pop(), like you did in the third piece of code.

In the second piece of code, you simply spelled animals wrong (you spelled it like "aniamls", which caused a NameError. You also assigned the return value of pets.append(value) (which is None, ie. not returning anything) to pets setting the value of pets to None, which made Python evaluate pets.append(value) in the second iteration of the for loop, and since pets is now None, Python cannot call the function append, a List function on it.

Answer to second question:

The line for value in animals: starts iteration through the list animals, which basically means that you are running the code in the for loop for every value in animals, and the variable value is assigned that value. Because you already have the value variable from the for loop, you don't need to create a value variable inside the for loop. Also, a_list.pop() returns the last value in a_list, so it won't solve your problem anyways. If you would like more information on the pop() function for a list, check out @mounika's answer.

The better way to solve your problem

The concise code you mentioned could be: pets = animals. This essentially copies the value of the animals variable into pets. But, this actually aliases animals to pets, which basically makes changing pets also change animals in the same way, which you may not want. That's why something like pets = animals[:] would be better, which makes a new variable pets that has its values separate from the values of animals. animals[:] basically is a new value which is a list that contains all the items in the list animals (essentially a copy of animals).

James Li
  • 1
  • 1
  • Welcome to Stack Overflow. Please add minimal reproducible example, or at least an attempt. See https://stackoverflow.com/help/how-to-ask – cards Aug 22 '21 at 22:22