30秒学会 Python 片段 · 2019年5月7日

30秒学会 Python 片段 – spread

Flattens a list, by spreading its elements into a new list.

Loop over elements, use list.extend() if the element is a list, list.append() otherwise.

代码实现

def spread(arg):
  ret = []
  for i in arg:
    ret.extend(i) if isinstance(i, list) else ret.append(i)
  return ret

使用样例

spread([1, 2, 3, [4, 5, 6], [7], 8, 9]) # [1, 2, 3, 4, 5, 6, 7, 8, 9]