2

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.

Desperado
  • 105
  • 9

2 Answers2

3

Use unpacking

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

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

In case python2

>>> list1.append(['wolf'] + list2)
rafaelc
  • 57,686
  • 15
  • 58
  • 82
1

Try this:

list1=[["wolf"]+list2]
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44