Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # flattens arbitrarily nested sequences
- # example: lst = [[1, 2, 3], [7, 3, 4], [2, 5, 6, [11, 12, 13]]]
- # returns: [1, 2, 3, 7, 3, 4, 2, 5, 6, 11, 12, 13]
- def flatten(items: Sequence, seqtypes=(list, tuple)) -> Sequence:
- """flattens arbitrarily nested lists
- :param :items sequence containing nested items
- :returns flattend result"""
- try:
- for i, x in enumerate(items):
- while isinstance(x, seqtypes):
- items[i:i+1] = x
- x = items[i]
- except IndexError:
- pass
- return items
Advertisement
Add Comment
Please, Sign In to add comment