Advertisement
phi2dao

flatten

Jan 17th, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.34 KB | None | 0 0
  1. # http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
  2.  
  3. def flatten(l, ltypes=(list, tuple)):
  4.   ltype, l = type(l), list(l)
  5.   i = 0
  6.   while i < len(l):
  7.     while isinstance(l[i], ltypes):
  8.       if not l[i]:
  9.         l.pop(i)
  10.         i -= 1
  11.         break
  12.       else:
  13.         l[i:i + 1] = l[i]
  14.     i += 1
  15.   return ltype(l)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement