简体   繁体   中英

How to add individual elements of list into another list in python?

For example i have the below list

list1 = [] 
list2 = ['cat', 'tiger']

I wanted to add only the individual elements in the list2 into list1. For eg,

list1.append(['wolf', list2])

And wanted the output like,

[[wolf, cat, tiger]]

But instead I get like,

[['wolf', ['cat', 'tiger']]]

I don't want list2 as such getting appended with the brackets, rather only the elements from the list2. Am i missing something ? Please add your comments. This is just an example from a bigger problem.

Use unpacking

>>> list1.append(['wolf', *list2])

[['wolf', 'cat', 'tiger']]

In case python2

>>> list1.append(['wolf'] + list2)

尝试这个:

list1=[["wolf"]+list2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM