1

What is a pythonic way for inserting multiple items into a list? Suppose I have :

a = [1,2,3]
b = ['a', 'b', 'c']

and I want b to be inserted into a, that is for example:

a = [1, 2, 'a', 'b', 'c', 3]

What I have done is:

a.insert(2,None)
a[2:3] = b

or also:

a = a[0:2] + b + a[2:]

The last approach seems nicer because it is just one line, but the problem is that I think a new list is created, and if the list is very large could be a performance issue.

On the other hand, the first approach doesn't seem very pythonic.

nadapez
  • 2,603
  • 2
  • 20
  • 26

0 Answers0