Maintaining Order


#1



animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index =  animals.index("duck") 
animals.insert(4,"cobra")
print animals

Oops, try again. The new item should have been inserted at the index previously occupied by "duck".

where am i wrong?


#2

oh i have solved this problem
add
animals = animals.insert(4,"cobra")


#3

Pardon me if I am being a busybody, but I saw your question and your own response to your question and I think that there is still something wrong.

I am not sure what you mean by adding animals = animals.insert(4, "cobra"), but for this exercise I would think that you are supposed to type this instead:

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")
animals.insert(duck_index, "cobra")
print animals


#4

I agree with umopapsidn.
# animals.insert(4,"cobra")
will not print the new list, even with the addition of
#animals=animals.insert(4,"cobra")


#5

Hello!
I have questions!
what is difference between yours and codeboy1111111 ???
why just animal.insert(2,"cobra") is wrong?
and why animals.insert(duck_index,"cobra") and animal = animal.insert(2,"cobra") are right?


#6

Hello,

There are 2 main differences between codeboy's code and what I am suggesting:

  1. codeboy mentions inserting the new animal, cobra, in position 4, while I suggest adding it to position 2 instead, which will place it in front of the "duck" term.

  2. From the way his code is phrased, I can infer that codeboy probably manually counted the position of duck, and then try to insert cobra by using the results of his manual calculation. Which I think is actually feasible as well (as long as the counting is done rightly), but to eliminate the possibility of miscalculation and to force Python to think in our stead, we can insert duck_index instead.

As for your later questions, I think that animals.insert(2, "cobra") and animals.insert(duck_index, "cobra") will both work since they follow the given format. However, animal.insert(2, "cobra") is incorrect because the list name is animals with an 's' and not animal without an 's'. And I don't think that animal = animal.insert(2, "cobra") makes sense to me, because I only learnt that the format for inserting something is simply list_name.insert(index, variable), so there are no equal signs etc involved.

Let me know what you think :slight_smile:


#7

Thank you for replying.
I totally understand what you said.
your explanation is so perfect!
I didn`t think that I am able to use duck_index instead of 2 before I saw it.
it is very helpful to me. Thanks a lot! :slight_smile:


#8

The aim of this exercise is to have the user write code that does the work of determining the index of the item, "duck", and then to have the code use that information to insert "cobra" into the appropriate place in the list. Therefore, while there are various ways to count indexes manually, and to use that information to pass the exercise, Codecademy's intent is to have it done this way ...

duck_index = animals.index("duck")
animals.insert(duck_index, "cobra")

So, @umopapsidn , your point is correct.


#9

I am really sorry, but I need your help.


I dont know what I am wrong.
I also don
t understand the instruction.
I hope it is not bothering you.


#10

Hi,

Don't worry, you're not bothering me at all :slight_smile: I am actually learning Python myself right now as my first programming language, so I can totally feel how you feel doing the exercises haha :stuck_out_tongue: But because of that sometimes I am not very confident of my answers, so the moderators on this site are really helpful as well :slight_smile:

Anyway, regarding that exercise, you can see that you have a list called start_list with 5 items, and another list called square_list which is an empty list. The instructions say that you are supposed to "write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list.
Then sort square_list!"

This also confused me at the start for like 5 seconds. So let me try to rephrase what I think the instructions are trying to get at:

  1. Write a for loop for start_list such that you can get the square of each number, and add the result that you obtain (i.e. the square of each number) to the currently empty list, square_list. I think the squaring of the numbers in start_list and the adding of these squares to square_list can be done in one line, so think about it :slight_smile:

  2. Sort square_list, which now contains the square of each number in start_list.

As for your image, I don't think it is right to say for square_list in start_list, because start_list does not contain square_list! You are supposed to use variables instead. For example, if you have a list letters = [a, b, c, d, e] and you want to apply a for loop for all the items in this list, you can say something like for alphabet in letters:

But your statement to sort square_list is correct :slight_smile: I just don't think that you need to indent it.

Hope this helps :slight_smile: Again, let me know what you think :slight_smile:

P.S. next time when you post a question, could you provide the link to the exercise too? As well as the name of the exercise. It will be easier for others to help you :slight_smile:


#11

Oh... very thank you for explanation.
I will try it along your advice.
and one more thing!
on the top, codeboy,
how can correct animals = animals,insert(4,'cobra').
is ti just one of many ways to solve problems?
I am suddenly curious it.


#12

I don't understand why codeboy said his answer is correct either :stuck_out_tongue: waits for moderator to appear


#13

haha yes!
thanks for replying! :slight_smile:


#14