0

I want to concatenate 2D lists to the end of a list_log, as follows:

list_log = []
list1 = [[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]] 
list2 = [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]
list_log.append(list1)
list_log.append(list2)

Desired result: list_log = [[0.0, 1.0], [1.7, 3.6], [8.4, 13.5], [20.1, 31.5], [29.3, 50.3], [41.8, 64.4], [74.1, 93.3], [61.9, 113.8]]

Actual result: list_log = [[[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]], [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]]

I've also tried getting this result using list comprehension, as follows:

list_log2 = [[[i, j] for i in list1[c] for j in list2[c]] for c in range(8)]

But this gives the following result: list_log2 = [[[0.0, 1.0]], [[1.7, 3.6]], [[8.4, 13.5]], [[20.1, 31.5]], [[29.3, 50.3]], [[41.8, 64.4]], [[74.1, 93.3]], [[61.9, 113.8]]], so with too many brackets.

Also, the example above uses only two lists, but in reality I have thousands of these lists coming in one after the other, and which I need to append to the end of the list_log one-by-one. Because of this I'm reluctant to use list comprehension as shown above, because this basically re-generates the entire log_list2 each time I append a new list, which isn't very efficient. That's why I'm trying to make this happen with .append() instead, as adding one element to the end of a list is computationally much less intensive than re-creating the entire log each time.

So ideally I'd like to make this work with .append() (or similar stuff like .extend()), but I'm open to all suggestions. Thanks in advance!

Qosa
  • 47
  • 5

2 Answers2

0

You can do it using zip() and a list comprehension:

[[*i, *j] for i, j in zip(list1, list2)]

Output:

[[0.0, 1.0],
 [1.7, 3.6],
 [8.4, 13.5],
 [20.1, 31.5],
 [29.3, 50.3],
 [41.8, 64.4],
 [74.1, 93.3],
 [61.9, 113.8]]
Nin17
  • 2,821
  • 2
  • 4
  • 14
  • Thanks! Can I ask you what the * in front of i and j does? – Qosa Jun 01 '22 at 08:39
  • the * will "explode" the list in its elements, but im not sure that this would be the best solution in this case ... – baskettaz Jun 01 '22 at 08:41
  • 1
    @baskettaz it is faster than your answer in the given case – Nin17 Jun 01 '22 at 08:43
  • @Qosa it's called unpacking here are the relevant docs https://peps.python.org/pep-0448/ – Nin17 Jun 01 '22 at 08:45
  • It doesn't "feel" ok, i haven't ment that the solution with list append would be faster :) +1 from me – baskettaz Jun 01 '22 at 08:45
  • Thanks! But If I want to append to a growing list_log rather than re-compute list_log each time a new list comes in, I need to do something like [[*i, *j] for i, j in zip(list_log, next_list)]. For this I need to initialize list_log = np.zeros((8,1)) instead of list_log = []. This works, but "feels" a bit messy. Any idea about how I could make your method work with log_list = [] as initialization? – Qosa Jun 01 '22 at 10:01
  • @Qosa you could try changing ```zip()``` to ```itertools.zip_longest()``` https://docs.python.org/3/library/itertools.html#itertools.zip_longest and setting ```fillvalue=0``` – Nin17 Jun 01 '22 at 10:05
  • @Qosa actually, it should be ```fillvalue=[0]``` – Nin17 Jun 01 '22 at 11:04
  • Hmm, both methods (initializing with np.zeros, or using itertools.zip_longest) have the disadvantage of introducing a first column filled with zeros in list_log, whereas I need list_log to have list1 as its first column... That's why I was looking for a solution using list_log.append( list# ), as this enables to append the first incoming list (list1) to an empty list_log rather than add it as a second column after a first zero-filled column. Again, this can be dealt with, but makes the code extra messy. – Qosa Jun 01 '22 at 11:22
  • @Qosa so why are you wanting to do it with an empty list then? just do it with list1 and list2 first as opposed to something and list1 – Nin17 Jun 01 '22 at 11:33
0
list1 = [[0.0], [1.7], [8.4], [20.1], [29.3], [41.8], [74.1], [61.9]]
list2 = [[1.0], [3.6], [13.5], [31.5], [50.3], [64.4], [93.3], [113.8]]

print([i+j for i,j in zip(list1,list2)])

>>>[[0.0, 1.0], [1.7, 3.6], [8.4, 13.5], [20.1, 31.5], [29.3, 50.3], [41.8, 64.4], [74.1, 93.3], [61.9, 113.8]]

baskettaz
  • 741
  • 3
  • 12