0

im trying to concatenate the content of an array

I have my array like this:

array = []
array = [[123,345],[567,789]]

I am using some code, but it shows me error most of the time and i dont know why, so I looking to change my code so it can works fine.

array = [[123,345],[567,789]]
final_array = reduce(operator.concat, array)
final_array = [123,345,567,789]

The result I need is this:

final_array = [123,345,567,789]

Any suggestions?? Thank you

  • That isn't an array. – Karl Knechtel Mar 22 '21 at 09:46
  • 1
    `reduce(operator.concat, array)` is a very inefficient algorithm. Don't use it, it is unnecessarily quadratic time when it is trivially implemented in linear time. – juanpa.arrivillaga Mar 22 '21 at 09:48
  • 1
    You can achieve this with `sum(array, [])` – Ritwik G Mar 22 '21 at 10:00
  • @RitwikG **no** Don't use that. That is 1) unnecessarily inefficient 2) not guaranteed to work. – juanpa.arrivillaga Mar 22 '21 at 10:02
  • @RitwikG this show me the error 'can only concatenate list (not "unicode") to list` so for now I continue with `final_array = list(itertools.chain(*array))`solution – Alex Hernan Mar 22 '21 at 10:03
  • @AlexHernan no, your code would not produce that error, and that method should work, but you should *never use it* anyway. – juanpa.arrivillaga Mar 22 '21 at 10:04
  • @AlexHernan also, use `itertools.chain.from_iterable(array)` instead of `itertools.chain(*array)` – juanpa.arrivillaga Mar 22 '21 at 10:04
  • @juanpa.arrivillaga Why do use say this is inefficient and not guaranteed to work? I am trying to understand the reason. I got this way of joining lists from one test case of sum from source code. But I couldn't find the implementation of the `sum`. If you have some insight and know where to look for this function definition please do let me know. Thank you – Ritwik G Mar 22 '21 at 10:07
  • 1
    @RitwikG because repeatedly concatenating makes the algorithm quadratic time. Because `+` is O(N). Also, if you read the documentation for `sum` it explicitly states it should only be used to sum numeric types, they have already purposefully prevented you from using this to concatenate strings (for the same reason that it is inefficient) and they could prevent you from doing it with lists in the future. – juanpa.arrivillaga Mar 22 '21 at 10:09
  • 1
    @RitwikG read the accepted answer to the linked duplicate above, the time complexity is explained in the accepted answer – juanpa.arrivillaga Mar 22 '21 at 10:10

1 Answers1

1

If using numpy as suggested by the term ‘array’ a simple solution would be:

a.flatten()

Where a is the array.

If using a list:

a = []
for i in final_array:
    a.extend(i)
S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • Thank you, I just resolved with `final_array = list(itertools.chain(*array))` – Alex Hernan Mar 22 '21 at 09:56
  • I’d recommend running `timeit` against this and the solution above to ensure the quickest method is being used. You might find that `chain` takes ~2x longer. – S3DEV Mar 22 '21 at 09:57
  • @S3DEV nah, `chain.from_iterable(a)` is probably faster than your loop, although both are algorithmically the same, and probably fast enough. – juanpa.arrivillaga Mar 22 '21 at 10:03
  • @juanpa.arrivillaga - Perhaps `chain` scales nicely; my tests were only on very small lists. Fair play. Thanks mate. – S3DEV Mar 22 '21 at 10:26