-1

Example 1

list1 = [[1,1,1],[1,1,1]]
list2 = [[0,0,0],[0,0,0]]

concated_list1_and_list2  = [[1,0,1,0,1,0],[0,1,0,1,0,1]]

Example 2

list1 = [[1,1,1],[1,1,1],[1,1,1]]
list2 = [[0,0,0],[0,0,0],[0,0,0]]

concated_list1_and_list2  = [[1,0,1,0,1,0],[0,1,0,1,0,1],[1,0,1,0,1,0]]

Example 3

list1 = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]]
list2 = [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

concated_list1_and_list2  = [[1,0,1,0,1,0],[0,1,0,1,0,1],[1,0,1,0,1,0],[0,1,0,1,0,1]]

I want to concatenate two lists in a decent manner states that without using loops such as concatenating along the axis or reshaping and again concatinating

  • 1
    item 1 concatenates differently than items 0 and 2? More info please (Also, I would this call merge, not concatenate, concatenate would be more like `[1,1,1,0,0,0]`) – Daniel Jun 07 '20 at 09:05
  • 2
    your example is weird and does not follow a specific pattern – Michael Hsi Jun 07 '20 at 09:07
  • Check out the updated example – Rishabh Chandaliya Jain Jun 07 '20 at 09:08
  • Just do it in a non-decent manner (for whatever you deem decent) and see whether it's fast enough for your purposes. If you can't get it to work, or it really needs to be faster, you'll have a better question. See [ask] and how to create a [mcve]. – Peter Wood Jun 07 '20 at 09:23
  • What constitutes a decent manner is a matter of opinion. Please be accurate. – Thierry Lathuille Jun 07 '20 at 09:23
  • 2
    In the first example, why is the second list `[0,1,0,1,0,1]`? It doesn't make sense. – Peter Wood Jun 07 '20 at 09:24
  • refer to this similar question here: https://stackoverflow.com/questions/3678869/pythonic-way-to-combine-two-lists-in-an-alternating-fashion – ruchi Jun 07 '20 at 09:34

3 Answers3

2

This has the same output than the examples:

def merge(list1, list2):
    result = []
    for index, (item1, item2) in enumerate(zip(list1, list2)):
        item = []
        for subitem1, subitem2 in zip(item1, item2):
            if index % 2 == 0:
                item.append(subitem1)
                item.append(subitem2)
            else:
                item.append(subitem2)
                item.append(subitem1)
        result.append(item)
    return result

or with list comprehension, imho less readable (based on Ch3steR's answer):

[[j for t in zip(*i) for j in t] if idx % 2 == 0 else [j for t in zip(*i) for j in reversed(t)] for idx, i in enumerate(zip(list1,list2))]
Daniel
  • 20,420
  • 10
  • 92
  • 149
0

you should say you want the element's order be determined by position in original list. A simple comprehension won't do it now, we need something that tracks the index

final_result = []
for index, (item1, item2) in enumerate(zip(list1, list2)):
    temp = []
    current_lists = zip(item1, item2) if index%2==0 else zip(item2, item1)
    [temp.append(element1), temp.append(element2) for element1, element2 in current_lists] 
    final_result.append(temp) 
Michael Hsi
  • 439
  • 2
  • 8
-1

The order will not be as you showed in examples, but it may seem as the simplest way.

list(a + b for a, b in zip(list1, list2))
Sazzy
  • 1,924
  • 3
  • 19
  • 27