0

So I have the following list of lists, and I'm trying to reduce the size by merging duplicates only if the lists items match exactly. order is significant (changing items order will be an issue). also they are not equal-sized lists.

Example:

List = [["a", "b", "c", "d", "e"], ["a", "b"], ["a", "b", "c", "d", "e", "f"], ["a"], ["a", "b", "c", "d", "e"], ["a", "b"]]

I'm expecting the following output:

List = [["a", "b", "c", "d", "e"], ["a", "b"], ["a", "b", "c", "d", "e", "f"], ["a"]]

this is the code:

def consolidate(all_list):
    a = {}
    for v in all_list:
        if len(v) > 0:
            k = v[0]
            if k not in a: a[k] = []
                a[k].append(v[1:])
    for k in a:
        a[k] = consolidate(a[k])
    return a

However, it does not seem to work.

Jau L
  • 904
  • 2
  • 8
  • 20
  • Possible duplicate of [Removing duplicates in lists](https://stackoverflow.com/q/7961363/4518341) - though avoid the solutions that use dicts or sets since lists aren't hashable – wjandrea Jun 22 '19 at 00:28
  • 2
    Is the order of the top-level list important? If it is, on Python >3.7, you can do: `list(map(list, dict.fromkeys(map(tuple, data))))`, and you can leave out the final mapping back to lists too. – juanpa.arrivillaga Jun 22 '19 at 00:45
  • 1
    @wjandrea you can easily convert to tuple – juanpa.arrivillaga Jun 22 '19 at 00:45

1 Answers1

2

Simply do :

output = []
for x in List:
    if x not in output : output.append(x)
output

Output

[['a', 'b', 'c', 'd', 'e'], ['a', 'b'], ['a', 'b', 'c', 'd', 'e', 'f'], ['a']]

Sebastien D
  • 4,369
  • 4
  • 18
  • 46