1

I have written the following code that works and the output is what it is expected :

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    temp=myList1[i]
    temp.insert(0,myList2[i])
    myList3.append(temp)
print(myList3)

But when I try to write the same code without the temp variable, it does not work.

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    myList3.append(myList1[i].insert(0,myList2[i]))
print(myList3)

And this code, also, does not work.

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    temp=myList1[i].insert(0,myList2[i])
    myList3.append(temp)
print(myList3)

I think there are some python fundamentals that I have not understood.

Any comments and help will be appreciated.

Thanks for answer

2
  • 1
    .insert returns None so you get None,None,None Commented Jul 22, 2021 at 11:49
  • Yes. I know that but why it returns None. Commented Jul 22, 2021 at 11:58

3 Answers 3

2

.insert is an inplace function. It returns None or nothing. If you don't want to use temp, you can use zip()

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i,j in zip(myList1,myList2):
    i.insert(0,j)
    myList3.append(i)
print(myList3)

A compact solution:

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]

myList3=[[j]+i for i,j in zip(myList1,myList2)]
print(myList3)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the keyword : inplace function
The sighingnow comment makes me in trouble (see its answer and comments). As I am a python beginner. I am going to read some docs about inplace function. I let you discuss with Sighingnow.
1

.insert of list returns None, rather than the result list.

To avoid the usage of temporary variable tmp, you could have

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    myList3.append(myList2[i:i+1] + myList1[i])
print(myList3)

Or

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    myList3.append([myList2[i]] + myList1[i])
print(myList3)

5 Comments

I see that. But why None is returned
This is how list.insert is implemented. And has noting to do with "inplace function". You could define a inplace insert function as python def insert(xs, idx, x): xs.insert(idx, x) return xs It is a inplace function, but it doesn't return None.
Your comment makes me in trouble. As I am a python beginner. I am going to read some docs about inplace function. I let you discuss with Sujay.
Inplace function means the function operates on its argument, but inplace function do could have meaningful returns.
Do you have link / blog / discussion to advise me ?
1

Take a look at your last example: The line: temp=myList1[i].insert(0,myList2[i]) does change your list "myList1"! The problem is, that the result of the insert will return "None".

So something like this would work, but not be pretty:

myList1=[[1,2],[3,4],[5,6]]
myList2=["abc","def","ghi"]
myList3=[]
for i in range(3):
    myList1[i].insert(0,myList2[i])
    myList3.append(myList1[i])
print(myList3)

1 Comment

Thanks for answer. Sujay give me the answer. insert is a inplace function

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.