Python Forum
List all possibilities of a nested-list by flattened lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List all possibilities of a nested-list by flattened lists (/thread-39477.html)



List all possibilities of a nested-list by flattened lists - sparkt - Feb-23-2023

S = [['he', 'she'], 'has', ['1', '2'], 'watch']

A list inside the list S indicates a possibility of a statement.
So there are four possibilities here:

'he has 1 watch'
'he has 2 watch'
'she has 1 watch'
'she has 2 watch'

(Just for demonstration so it doesn't need to be grammatically correct)

I'm sure there's a simple way to list out all possibilities, but how? Any help would be much appreciated!


RE: List all possibilities of a nested-list by flattened lists - sparkt - Feb-23-2023

Got that!

import itertools
S = [['he', 'she'], ['has'], ['1', '2'], ['watch']]
print(list(itertools.product(*S)))

Output: [('he', 'has', '1', 'watch'), ('he', 'has', '2', 'watch'), ('she', 'has', '1', 'watch'), ('she', 'has', '2', 'watch')]