Python Forum
Problem with Lists in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Lists in Python
#1
Thanks for answering my question in advanced... Sorry If I sound Stupid. Wink

I popped an element from the middle of a list in python how do I re-insert it?

To be specific, I was learning to use Lists in Python. I popped an element from the middle of a list, but I am unable to figure out that how to re-insert or insert another element in between my list?

for example, I did
new_list.pop(0)
and it popped the zeroth element but then I tried to re-insert the same element and I was unsuccessful, I don't know if there are any attributes that I don't know yet that does this but what if I popped an element from the middle of a List and I wanted to re-insert it to the same place, or If I wanted to add another element in the middle of my list, How exactly would I do that?

I am posting an image to clarify that... Link -->http://prntscr.com/kmt8z9
Reply
#2
>>> lst=[1, 2, 4]
>>> lst.insert(2, 3) # insert 3 at index 2. indexing is 0-based
>>> lst
[1, 2, 3, 4]
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks Man, for the answer... now I understand it :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 613 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
Star --- Python lists and Linked Lists --- sabe 3 2,648 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Newbie lists problem LedDiode 5 3,608 Dec-16-2018, 08:33 PM
Last Post: LedDiode

Forum Jump:

User Panel Messages

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