flattening list

Abhishek Mishra ideamonk at gmail.com
Sun May 9 04:38:29 EDT 2010


thanks for the excercise just figured out this -

#!/usr/bin/env python
import sys
sys.setrecursionlimit(2000)

def flatten(l):
  flattened = []
  for i in l:
    if type(i) == type([]):
      flattened += flatten(i)
    else:
      flattened.append(i)
  return flattened




if __name__=='__main__':
  p=[1,[2,3,4],[5,6,],9,[[11,12]]]
  print flatten(p)

But a google search will lead you to more elegant generic solutions :)


On Sun, May 9, 2010 at 1:46 PM, gopi krishna <dasarathulagopi at gmail.com>wrote:

> Hi ,
>     Anyone can pls help me in flattening the list.
> if p is the my list which is defined below
> p=[1,[2,3,4],[5,6,],9,[[11,12]]]
> from the above how to get a list
> as [1,2,3,4,5,6,9,11,12]
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100509/af822349/attachment-0001.html>


More information about the Python-list mailing list