-3

how do I insert list 'lst' into the first possition of the 'n_lst'?

n_lst = [['I', 'dont'], ['know', 'what']]
lst = ['whatever', 'whatever2']

what I need:

[['whatever', 'whatever2'], ['I', 'dont'], ['know', 'what']]
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types – MattDMo Oct 08 '20 at 20:23
  • 1
    `n_lst.insert(0, lst)` – S3DEV Oct 08 '20 at 20:23
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). Among other things, we expect you to do appropriate research before posting here. This is already answered in many tutorials on line and in several existing Stack Overflow questions. – Prune Oct 08 '20 at 20:24

3 Answers3

0

If you want a new list, you can:

>>> result = [lst] + n_lst
>>> result
[['whatever', 'whatever2'], ['I', 'dont'], ['know', 'what']]

If you need to insert lst inside n_lst

>>> n_lst.insert(0, lst)
>>> n_lst
[['whatever', 'whatever2'], ['I', 'dont'], ['know', 'what']]

Joan Puigcerver
  • 104
  • 1
  • 13
0

You can use list1.insert(index, list2). In your case: n_lst.insert(0, lst).

QLR
  • 1
  • 3
0

Use insert to do this:

n_lst.insert(0, lst)
Darcey BM
  • 301
  • 1
  • 5
  • 20