![](/img/trans.png)
[英]how to add elements of a list into another list with no bracket and no imports in python?
[英]How to add individual elements of list into another list in python?
例如我有以下列表
list1 = []
list2 = ['cat', 'tiger']
我只想將list2中的各個元素添加到list1中。 例如
list1.append(['wolf', list2])
並希望輸出像
[[wolf, cat, tiger]]
但是我反而變得
[['wolf', ['cat', 'tiger']]]
我不希望將list2這樣加上括號,而只希望list2中的元素。 我想念什么嗎? 請添加您的評論。 這只是更大問題的一個例子。
使用開箱
>>> list1.append(['wolf', *list2])
[['wolf', 'cat', 'tiger']]
萬一python2
>>> list1.append(['wolf'] + list2)
嘗試這個:
list1=[["wolf"]+list2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.