1

I have a list:

list1=[[2,3],[4,5],[6,7]]

I want to add a single value "one" to all sublist in the list at index 2

final output should be:

list2=[[2,3,"one"],[4,5,"one"],[6,7,"one"]]

tried with:

for list2 in list1:
    print list2.insert(2,"one")

But its showing error as None.

quamrana
  • 37,849
  • 12
  • 53
  • 71
learningstudent
  • 577
  • 1
  • 4
  • 14
  • ``list.insert`` does not return anything – Mike Scotty Jul 31 '18 at 08:15
  • @MikeScotty: list.insert is inserting new list but not adding values in sublist – learningstudent Jul 31 '18 at 08:23
  • 1
    Don't say "add" when you really mean "append"... or "insert at index 2"... You're not "adding 1", you're "inserting 'one' at index 2". Very misleading title. – smci Jul 31 '18 at 08:33
  • The `None` is due to `list.insert` being an in-place function. There are many duplicates e.g. [Insert an element at specific index in a list and return updated list](https://stackoverflow.com/questions/14895599/insert-an-element-at-specific-index-in-a-list-and-return-updated-list) – smci Jul 31 '18 at 08:41

7 Answers7

4

list.insert is an in-place operation and returns None. Instead, you can use a list comprehension:

L = [[2,3],[4,5],[6,7]]

res = [[i, j, 'one'] for i, j in L]

print(res)

[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
jpp
  • 159,742
  • 34
  • 281
  • 339
3

Change your code as below :

list1=[[2,3],[4,5],[6,7]]

for subList in list1:
    subList.append("one")

print list1

Output is :

[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
3

You need:

list1=[[2,3],[4,5],[6,7]]

for l in list1:
    l.append("one")

Output:

[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
3

The list.insert() function modifies the list without returning it.

You should print the list afterwards:

for list2 in list1:
    list2.insert(2,"one")
print(list1)

Alternatively, what you want to do can be more easily achieved with list comprehension:

list1=[[2,3],[4,5],[6,7]]
list2=[i + ["one"] for i in list1]
print(list2)

Both pf the above output:

[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
blhsing
  • 91,368
  • 6
  • 71
  • 106
2

Just use list comprehension

>>> list1 = [[2,3],[4,5],[6,7]]
>>> [e + ['one'] for e in list1]
[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
Sunitha
  • 11,777
  • 2
  • 20
  • 23
1

It's because list.insert in python does not return any value. So your code inserts value to list correctly but you try to print this list wrong way. It should be:

for list2 in list1:
    list2.insert(2,"one")
    print list2
running.t
  • 5,329
  • 3
  • 32
  • 50
0
List_1 = [[2,3],[4,5],[6,7]]  
List_2 = [ ]
For i in List_1:             #Selecting a sub-list at each iteration
    temp = i.append('one')   #Adding 'one' to the sub-list
    List_2.append()          #Adding the modified sublist to List_2

Result:

Print(List_2)    
[[2, 3, 'one'], [4, 5, 'one'], [6, 7, 'one']]
Sociopath
  • 13,068
  • 19
  • 47
  • 75