Python : concat arrays of groupped lines
Question: I have the following array:

    
    
    [
      ["String 0", [1, 2]],
      ["String 1", [1, 3]],
      ["String 2", []],
      ["String 3", [2]],
      ["String 1", [1, 2]],
      ["String 2", [0]]
    ]
    

I need to transform it into an array with unique Strings (first column) and
concatenated second column.

Here is what output should look like:

    
    
    [
      ["String 0", [1, 2]],
      ["String 1", [1, 3, 1, 2]],
      ["String 2", [0]],
      ["String 3", [2]]
    ]
    

In this case, Strings are unique and `"String 1"` has a second column `"1, 3,
1, 2"` and `"String 2"` has second column `"0"`.

I know how to unique an array but not how to group and concatenate at the same
time.

Thank you.


Answer: Like the IanAuld suggests, a dictionary is likely the best way to handle this.

Try the following

    
    
    arr_of_arrs = [
        ["String 0", [1, 2]],
        ["String 1", [1, 3]],
        ["String 2", []],
        ["String 3", [2]],
        ["String 1", [1, 2]],
        ["String 2", [0]]
      ]
    from collections import defaultdict
    arrs = defaultdict(lambda: [])
    for arr in arr_of_arrs:
        arrs[arr[0]] += arr[1]
    

How to convert the dictionary back to your array of arrays is left as an
exercise.

