-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

7

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
  • 50,081
  • 9
  • 65
  • 94
4
l = []
for x,y in zip(list_1,list_2):
    l.append(x)
    l.append(y)
Nilo Araujo
  • 725
  • 6
  • 15
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,496
  • 1
  • 12
  • 18