0

list_of_lists=[[1,2,3],[4,5,6]]
list_to_add=["A","B","C"]

I would like the result to be that list_of_lists will become:
[["A","B","C"],[1,2,3],[4,5,6]]

Thanks!

Jimmy
  • 127
  • 10

2 Answers2

1

You can use append to add an element to the end of the list, but if you want to add it to the front (as per your question), then you'll want to use fooList.insert( INSERT_INDEX, ELEMENT_TO_INSERT )

Explicitly

>>> list_of_lists=[[1,2,3],[4,5,6]]
>>> list_to_add=["A","B","C"]
>>> list_of_lists.insert(0,list_to_add) # index 0 to add to front
>>> print list_of_lists
[['A', 'B', 'C'], [1, 2, 3], [4, 5, 6]]

There is more information regarding List API here.

Locke
  • 374
  • 2
  • 9
0

You can use (append) to do it like this:

list_of_lists=[[1,2,3],[4,5,6]]
list_to_add=["A","B","C"]

list_of_lists.append(list_to_add)

And you should get the intended result.

>>> list_of_lists
[[1, 2, 3], [4, 5, 6], ['A', 'B', 'C']]

In case you want it in the beginning you could wrap the "list_to_add" into a list then add to it the elements of "list_of_lists" like this:

outpu_list = [list_to_add] + list_of_lists
>>> output_list
[['A', 'B', 'C'], [1, 2, 3], [4, 5, 6]]

Otherwise if you want the freedom to insert at any index check @Locke's answer.

D.Manasreh
  • 900
  • 1
  • 5
  • 9