Answer the question
In order to leave comments, you need to log in
How to organize a list into one line in Python?
How to write the list calculation code in one line? There is this code:
a_list = [1, [2, 3]]
new_list = []
for el in a_list:
if isinstance(el, list):
for inner_el in el:
new_list.append(inner_el)
else:
new_list.append(el)
new_list = [inner_el if isinstance(el, list) else el for el in a_list for inner_el in el]
new_list = [el if not isinstance(el, list) else inner_el for el in a_list for inner_el in el]
Answer the question
In order to leave comments, you need to log in
new_list = sum( (item if isinstance(item, list) else [item] for item in a_list) , [])
new_list = []
for item in a_list:
if isinstance(item, list):
new_list.extend(item)
else:
new_list.append(item)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question