7/14 Mantianing Order need help


#1

I’ve understood what this section taught, but then I stuck when I tried to add another insert.
Below is what I add (bold ones).

animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
abcds = animals.index(“fennec fox”)
Your code here!
animals.insert(duck_index,“cobra”)
animals.insert(abcds,“can”)
print animals # Observe what prints after the insert operation

print result:
[‘aardvark’, ‘badger’, ‘duck’, ‘can’, ‘emu’, ‘fennec fox’]

And then I found that the “can” is before “emu” instead of “fennec fox”(always insert ahead by 2 instead of 1 place), this is where I stuck.
But if I assign any other string before “duck” to adcds, or if I delete ‘animals.insert(duck_index,“cobra”)’, the “can” would be fine again as standing just before the ‘abcds’ string.

Thank you very much if someone could solve my problem!


#2

You’re mentioning lots of different operations, which one isn’t behaving like you expect?
list’s insert method inserts at the specified location, nothing affects which that location is except what you specified. If you insert at index 5, that is where it goes (as long as the list is large enough that this is at all possible)

Perhaps you’d like to print out the list before the insertion, and print out where and what you insert, and then what the list looks like afterwards.

Your code looks like it inserts the string "cobra" but it’s not in your output, so what you posted doesn’t quite match up with your output. Note that if one copies code from your post and attempts to run it, one will get a syntax error so it isn’t possible to reproduce.
Any code has to be a perfect copy to be useful for discussion, otherwise you and everyone else probably aren’t talking about the same code, or at best are talking about code that doesn’t do anything.


#3

Hi ionatan, I am sorry to copy a wrong output, the result was [‘aardvark’, ‘badger’, ‘cobra’, ‘duck’, ‘can’, ‘emu’, ‘fennec fox’].

Here let me put the steps I did.

  1. first, I follow the instructions to type the code needed:

animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
Your code here!
animals.insert(duck_index,“cobra”)
print animals # Observe what prints after the insert operation

output:
[‘aardvark’, ‘badger’, ‘cobra’, ‘duck’, ‘emu’, ‘fennec fox’]

  1. it runs good, and then I want to add another insert to see how they are located.
    so I added the abcds, and assigned “emu” to it, and insert a “can” before it:

animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
abcds = animals.index(“emu”)
Your code here!
animals.insert(duck_index,“cobra”)
animals.insert(abcds,“can”)
print animals # Observe what prints after the insert operation

output:
[‘aardvark’, ‘badger’, ‘cobra’, ‘can’, ‘duck’, ‘emu’, ‘fennec fox’]

  1. I found there’s the “duck” between “can” and “emu”, unliked the situation when only insert one at a time, so I assigned “fennec fox” to abcds to check:

animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
abcds = animals.index(“fennec fox”)
Your code here!
animals.insert(duck_index,“cobra”)
animals.insert(abcds,“can”)
print animals # Observe what prints after the insert operation

output:
[‘aardvark’, ‘badger’, ‘cobra’, ‘duck’, ‘can’, ‘emu’, ‘fennec fox’]

  1. the result is the same, so I assigned “badger” to abcds:

animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
abcds = animals.index(“badger”)
Your code here!
animals.insert(duck_index,“cobra”)
animals.insert(abcds,“can”)
print animals # Observe what prints after the insert operation

output:
[‘aardvark’, ‘can’, ‘badger’, ‘cobra’, ‘duck’, ‘emu’, ‘fennec fox’]

This time it came to my expectation, the “can” is just before the assigned abcds(“badger”).

Thus I could not understand that, why the second insert(“can”) didn’t show as expected when the specified location(abcds/“emu” or “fennec fox”) is after the first one(duck_index/“duck”)?
How they are affected when there exist more than one insert?

Thank you sincerely!


#4

the .index() method will give you the index (integer value) of those elements at that point in time, which we can see if we print those variables:

print abcds

if we then start inserting values into the list, the values stored in abcds and duck_index won’t change.

maybe its good to insert some print statements so you can see values (like indexes) of variables?


#5
['aardvark', 'badger', 'cobra', 'duck', 'emu', 'fennec fox']
     0          1         2       3       4         5

You insert at index 3. The current value at 3, and all that follow, will move one step to the right, and your value is inserted at 3

If you wanted to insert in front of emu. you would need to insert at index 4, that’s where emu currently is.

If you have a list like so:

['abc']

Then 'abc' is at index 0. If you put a hundred 'xyz' in front of it, then its new position is 100


#6

I understand the basic situation. But for the second step, I did not put “can” before “duck”, I wanted it before “emu”, and then output is , ‘can’, ‘duck’, ‘emu’, , and it returned fine if I wanted it before “badger”.

so the question is, I donot konw why, for the latter strings to assigned the abcds, there always be a string in between? Maybe it’s the double inserts at one time cause it? (I used specified string instead of number.)


#7

It inserts exactly where you say. There’s nothing funny going on here.
Consider where you said to insert. (Reminder: printing before and after the insertion along with the location of insertion would give you the information you need)


#8

I just quickly run through the scenarios in steps, and added a couple of returns checking the index value to make it clear. Have a look and see if there is some confusion in your assumptions or understanding of the exercise. Hope it helps.

#-----Step 1 Insert “cobra” to the index value of “duck”------
animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”

now duck_index = 2

animals.insert(duck_index,“cobra”)
print animals
#result is [“aardvark”, “badger”, “cobra”, “duck”, “emu”, “fennec fox”]

“”"------Step 2 insert “cobra” to index number of “duck” & insert “can” to the index number of “emu”-------"""
animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”) # Use index() to find “duck”
#now duck_index = 2
abcds = animals.index(“emu”)
#now abcds = 3
animals.insert(duck_index,“cobra”) #insert “cobra” at index 2
animals.insert(abcds,“can”) #insert “abcds” at index 3
print animals # Observe what prints after the insert operation
#return is [“aardvark”, “badger”, “cobra”, “can”, “duck”, “emu”, “fennec fox”]

“”"-----Step 3 insert “cobra” to index number of “duck” and insert “can” to index number of “fennec fox”-------"""
animals = [“aardvark”, “badger”, “duck”, “emu”, “fennec fox”]
duck_index = animals.index(“duck”)# Use index() to find “duck”
#duck_index = 2
abcds = animals.index(“fennec fox”)
#abcds = 4
animals.insert(duck_index,“cobra”) # insert “cobra” at index 2
animals.insert(abcds,“can”) #insert “can” at index 4
print animals # Observe what prints after the insert operation
#return is [“aardvark”, “badger”, “cobra”, “duck”, “can”, “emu”, “fennec fox”]


#9

very clear! Thank you very much
And I figured out that if I still want “can” just before “fennec fox”, I should just put
abcds = animals.index(“fennec fox”)
#before
animals.insert(abcds,“can”)
#and after
animals.insert(duck_index,“cobra”)


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.