Python Forum
myList.insert(index, element) question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
myList.insert(index, element) question
#1
Why does the "insert.(index, element)" function work backwards in this for loop when used in the context "myList.insert(1, myList[v])"? Instead of adding 0, 1, and 2 to the list to the "myList[1]" location during iteration it will instead add 1 to myList[0], myList[1], and myList[2] during iteration.

Used outside of the loop "myList.insert(4, 8)" it will behave as expected adding 8 to "myList[4]" location.

myList = [1,2,3]
print("This is my initial list: ", myList)
for v in range(len(myList)):
    print("This is v: ", v)
    myList.insert(1, myList[v])
    print("This is my list during iteration: ", myList)
print(myList)

myList.insert(4, 8)
print(myList)
I couldn't upload an image of my output, but here is a copy/paste view of my output.

Output:
This is my initial list: [1, 2, 3] This is v: 0 This is my list during iteration: [1, 1, 2, 3] This is v: 1 This is my list during iteration: [1, 1, 1, 2, 3] This is v: 2 This is my list during iteration: [1, 1, 1, 1, 2, 3] [1, 1, 1, 1, 2, 3] [1, 1, 1, 1, 8, 2, 3]
Larz60+ write Aug-27-2021, 03:19 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Refrain from using images (for code).
Your code partially fixed this time. Please use bbcode tags on future posts.

Attached Files

Thumbnail(s)
   
Reply
#2
What do you mean by "work backward"? The action is a bit confusing because you're modifying a list at the same time as you're reading it for information.

Start with [1, 2, 3].
First time through the loop, you tell it copy element 0 (which is a 1) to spot 1. -> [1, 1, 2, 3]
Next time through the loop, you tell it copy element 1 (which is now a 1) to spot 1 -> [1, 1, 1, 2, 3]
Next time through the loop, you tell it copy element 2 (which is now a 1) to spot 1 -> [1, 1, 1, 1, 2, 3]

It doesn't add 1, 2, 3 because myList has changed by the time the element is looked for.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How i can add elements to table index of element blazej2533 3 679 Dec-03-2020, 08:16 PM
Last Post: Larz60+
  How to get index of minimum element between 3 & 8 in list Mekala 2 926 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Select a part of an element of a list with Index BollerwagenIng 0 994 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng
  Unable to locate element no such element gahhon 6 2,052 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Newbie question for bulk insert into SQL Server database zydjohn 6 9,635 Dec-14-2017, 11:04 PM
Last Post: Larz60+
  Newbie question for sum the specific element in named tuple zydjohn 1 2,850 Dec-12-2017, 07:29 PM
Last Post: buran
  Change single element in 2D list changes every 1D element AceScottie 9 7,144 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 10,789 Jul-21-2017, 07:39 PM
Last Post: nilamo
  Python - Search element in list without the value index salmanfazal01 1 2,810 Nov-04-2016, 02:07 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020