What's the idiomatic syntax for prepending to a short python list?

后端 未结 5 1589
清酒与你
清酒与你 2020-11-27 09:42

list.append() is the obvious choice for adding to the end of a list. Here\'s a reasonable explanation for the missing list.prepend(). Assuming my

5条回答
  •  有刺的猬
    2020-11-27 10:13

    If you can go the functional way, the following is pretty clear

    new_list = [x] + your_list
    

    Of course you haven't inserted x into your_list, rather you have created a new list with x preprended to it.

提交回复
热议问题