2

I have a list called ilist which looks like this:

    ['TCS', 1968, ['Mumbai', 'Delhi', 'Kochi', 'Banglore', 'Pune'], 'Wipro', 1981, ['Pune', 'Banagalore', 'Pune']]

Now I want to insert some additional values in a form of list something like highlighted one below:

    ['TCS', 1968, ['Mumbai', 'Delhi', 'Kochi', 'Banglore',**["ITPL","Whiteflied"]**, 'Pune'], 'Wipro', 1981, ['Pune', 'Banagalore', 'Pune']]

I thought to try the insert method, which I was able to use to insert 2nd nested loop like this:

    ilist.insert(2,['Mumbai','Delhi','Kochi','Banglore','Pune'])

But as the insert method take only one argument, i.e the index where we want to insert the value. How we can give it like below:

    ilist.insert(2,3,"ITPL","Vedhi","Electronic city") 

this is giving error. Which I know will always come:

Traceback (most recent call last):
  File "G:/Python Programs/Loops.py", line 41, in <module>
ilist.insert(2,3,"ITPL","Vedhi","Electronic city")
TypeError: insert() takes exactly 2 arguments (5 given)

So what is the alternative method I can use to achieve the same? Apart from declaring a fresh list.

1 Answer 1

5

You have to index the first list and then insert into the list at that index:

ilist[2].insert(3, ["ITPL","Vedhi","Electronic city"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.