import itertools
a=[[1, 2, 3], [4, 5, 6]]
flat_list = list(itertools.chain.from_iterable(a))
print(flat_list)
# Output:
[1, 2, 3, 4, 5, 6]
def flatten(x):
if isinstance(x, list):
return [a for i in x for a in flatten(i)]
else:
return [x]
def flatten(t):
return [item for sublist in t for item in sublist]
# If you know the index of the item you want, you can use the pop() method. It modifies the list and returns the removed item.
L = ['a', ['bb', 'cc', 'dd'], 'e']
x = L[1].pop(1)
print(L)
# Prints ['a', ['bb', 'dd'], 'e']
# removed item
print(x)
# Prints cc