0
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2
print(list3)

output ---

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

but I want output like this ---

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

please help me

alani
  • 12,573
  • 2
  • 13
  • 23

3 Answers3

5

You can create a for loop and use zip and extend:

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = []
for items in zip(list1, list2):
    list3.extend(items)

print(list3)

Alternatively a list comprehension (however it is both slower and less readable, still will keep it here just for information as to how to handle cases where an iterable has to be extended in a comprehension)

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = [c for items in zip(list1, list2) for c in items]
print(list3)
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • And of the two, the `for` loop is the easier to read and might even be faster. – alani Dec 01 '21 at 19:40
  • 1
    @alani indeed, just timed both and the loop was faster, as well as it is more readable, will change the order in answer – Matiiss Dec 01 '21 at 19:45
  • I doubt these two different approaches are materially different in terms of performance. List comprehensions are generally *marginally* faster than the equivalent loop, mostly due to caching the `.append` method resolution, which can be accomplished using a loop doing something like `append = result.append` – juanpa.arrivillaga Dec 01 '21 at 20:35
  • Ah, although in this case, setting up the nested iteration for each pair is a high overhead, which probably explains the the comprehension loosing. – juanpa.arrivillaga Dec 01 '21 at 20:38
  • 1
    @juanpa.arrivillaga well, `timeit` showed that the loop was slightly faster, my thought was the same, it doesn't use a nested loop (perhaps internally it uses some looping mechanism but that is likely then done in c) – Matiiss Dec 01 '21 at 20:39
  • @juanpa.arrivillaga I was referring to the `extend` method – Matiiss Dec 01 '21 at 21:04
  • @Matiiss ah, yes, I was referring to the list comprehension! We agree – juanpa.arrivillaga Dec 01 '21 at 21:04
1
from itertools import chain
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

result = list(chain(*zip(list1, list2)))
print(result)

As a [better] alternative (because argument you pass to .from_iterable() will be evaluated lazily), as mentioned by @juanpa.arrivillaga:

result = list(chain.from_iterable(zip(list1, list2)))
buran
  • 13,682
  • 10
  • 36
  • 61
  • you should use `chain.from_iterable` instead – juanpa.arrivillaga Dec 01 '21 at 19:28
  • @juanpa.arrivillaga, as mentioned in the docs - it's an alternative constructor. Are there any benefits in using it vs `*zip()`? I updated my answer, though. – buran Dec 01 '21 at 19:32
  • 2
    `chain.from_iterable` iterates over the `zip` object itself, rather than having to expand the entire sequence into memory in order to pass each element to `chain`. – chepner Dec 01 '21 at 19:38
  • Yeah, it says, _evaluated lazily_. I should have figured it out myself. thanks, @chepner. – buran Dec 01 '21 at 19:43
  • Well both `chain` and `chain.from_iterable` will return a lazy iterator, but whenever you do `f(*args)` the. `args` will create a tuple from all the arguments, in this case, a potentially large one, and it removes the benefits of a lazy chain. – juanpa.arrivillaga Dec 01 '21 at 19:56
  • @juanpa.arrivillaga, I think we both say the same thing (after I figured your point with help of chepner). Of course both constructors will return [lazy] `chain` object.The sentence in the docs - _Gets chained inputs from a single iterable argument that is evaluated lazily_ - I think _evaluated lazily_ refers to the argument being evaluated lazily. – buran Dec 01 '21 at 20:09
  • 1
    @buran right, just wanted to be clear for people reading this, I assumed you understood exactly – juanpa.arrivillaga Dec 01 '21 at 20:32
0

A brute-force merge will go something like this:

list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
list3 = []

## merge two list with output like ['a',1,'b',2,'c',3]
for i in range(len(list1)):
    list3.append(list1[i])
    list3.append(list2[i])

print(list3)

A better way is to use itertool or chain as above!