I have what is probably a trivial Python question, I have a list of lists of tuples in the following format:
lis1=[[(1,2),(3,4)],[(5,6),(7,8)]]
which I want to convery to a single list of tuples with ideally a space between the lists, like so:
lis2=[1,2,3,4, ,5,6,7,8]
or simply as a single list:
lis3=[1,2,3,4,5,6,7,8]
what would be the fastest way to achieve this?
Thanks!
Edit
So far I have tried:
lis3=[w for w in term for term in lis1]
and a for loop
lis3=[]
for term in lis1:
for w in term:
lis3.append(w)
which works, but I am really looking for a single line implementation and to avoid the .append() function.
Answer
you've mixed outer and inner loops in the list comprehension. Imagine you have a multiline code: for inner_list in outer_list: for item in inner_list: result.append(item) then the same as a list comprehension: result = [item for inner_list in outer_list for item in inner_list] note: the order of the loops is exactly the same. – J.F. Sebastian