Menu

gitpiper

spread python Code Snippet in 2024

listintermediate

Last Updated: 14 December 2024

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]

python snippet similar to spread For You in December 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️