-1

How do I criss cross two lists together in python? Example:

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

Expected Outcome:

combined_list = [1, 4, 2, 5, 3, 6]

3 Answers3

6

A pythonic way of doing this:

[item for sublist in zip(a,b) for item in sublist]

Per the request, if you only want a list if the two lists are the same length, you can use:

[item for sublist in zip(a,b) for item in sublist if len(a) == len(b)]

And see if the result is an empty list.

user3483203
  • 48,205
  • 9
  • 52
  • 84
4
l = []
for x,y in zip(list_1,list_2):
    l.append(x)
    l.append(y)
Nilo Araujo
  • 487
  • 4
  • 12
3

using itertools

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]

new_list = list(itertools.chain.from_iterable(zip(list_1,list_2))) 
# [1, 4, 2, 5, 3, 6]
Skycc
  • 3,405
  • 1
  • 9
  • 17