Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Latest commit

 

History

History
23 lines (19 loc) · 484 Bytes

spread.md

File metadata and controls

23 lines (19 loc) · 484 Bytes
title type tags cover dateModified
Spread list
snippet
list
digital-nomad-14
2020-09-15 16:13:06 +0300

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]