Jeremy Jeremy - 3 months ago 14
Python Question

How do you convert a list of nested lists into a list of lists with unique data?

After looping my brains out creating reference dictionaries and multi-nested lookup lists I've decided that there has to be an easier way to do this. I can't be the first person to do this type of conversion. I don't even know where to start looking in the docs for a solution.

I have a system that is outputting the following data.

initial_data = [
[21,[[1],[2,3],[6],[7]]],
[22,[[4,5],[6,7]],
[23,[[1],[4,5],[6],[7]]],
[24,[[1],[2,3,4],[6],[7]]],
]


I have another system that expects the data in the following format (order does not matter).

return_data = [
[21,[1,2,6,7]],
[21,[1,3,6,7]],
[22,[4,6]],
[22,[4,7]],
[22,[5,6]],
[22,[5,7]],
[23,[1,4,6,7]],
[23,[1,5,6,7]],
[24,[1,2,6,7]],
[24,[1,3,6,7]],
[24,[1,4,6,7]],
]

Answer

You can use the itertools.product, which produces

Cartesian product of input iterables.

Roughly equivalent to nested for-loops in a generator expression. For example, product(A, B) returns the same as ((x,y) for x in A for y in B).

Use it on the second element of each sublist should produce what you need:

from itertools import product
[[k, p] for k, v in initial_data for p in product(*v)]

# [[21, (1, 2, 6, 7)],
#  [21, (1, 3, 6, 7)],
#  [22, (4, 6)],
#  [22, (4, 7)],
#  [22, (5, 6)],
#  [22, (5, 7)],
#  [23, (1, 4, 6, 7)],
#  [23, (1, 5, 6, 7)],
#  [24, (1, 2, 6, 7)],
#  [24, (1, 3, 6, 7)],
#  [24, (1, 4, 6, 7)]]
Comments