I have a block of code that gives me a list that has some triple nested lists within it:
my_list = [[['item1','item2']], [['item3', 'item4']]]
And I would like to make it:
my_list = [['item1','item2'], ['item3', 'item4']]
Any suggestions?
A simple, but efficient way is to flatten your triple nested list with itertools.chain.from_iterable
:
>>> import itertools
>>> my_list = [[['item1','item2']],[['item3','item4']]]
>>> my_list = list(itertools.chain.from_iterable(my_list))
>>> my_list
[['item1', 'item2'], ['item3', 'item4']]
Which has O(n)
complexity for a list of size n
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With