Re: [Tutor] Recursively flatten the list

2011-03-26 Thread Dave Angel
On 01/-10/-28163 02:59 PM, Prasad, Ramit wrote:" >A more important problem is that it is flattening only one level. >Multi-level flattening is I think not possible without using some kind >of recursion." > >Not true, just requires more iterations to check each element. Each >iteration could chec

Re: [Tutor] Recursively flatten the list

2011-03-25 Thread Prasad, Ramit
" A more important problem is that it is flattening only one level. Multi-level flattening is I think not possible without using some kind of recursion." Not true, just requires more iterations to check each element. Each iteration could check if every element is a list and then unpack if it is a

Re: [Tutor] Recursively flatten the list

2011-03-24 Thread Andre Engels
2011/3/24 Rafael Durán Castañeda : > I can do it with two list comprehensions: > list_ = [1, 2, [3, 4], 5, [6, 7, 8], 9] [x[i] for x in list_ if isinstance(x, list) for i in range(len(x))] + [x for x in list_ if not isinstance(x, list)] > [3, 4, 6, 7, 8, 1, 2, 5, 9] > > But i lo

Re: [Tutor] Recursively flatten the list

2011-03-24 Thread Rafael Durán Castañeda
I can do it with two list comprehensions: >>> list_ = [1, 2, [3, 4], 5, [6, 7, 8], 9] >>> [x[i] for x in list_ if isinstance(x, list) for i in range(len(x))] + [x for x in list_ if not isinstance(x, list)] [3, 4, 6, 7, 8, 1, 2, 5, 9] >>> But i loose original order, Can anyone do it with just one

Re: [Tutor] Recursively flatten the list

2011-03-24 Thread Tom Zych
Dharmit Shah wrote: > I am learning Python and yesterday I cam across a definition wherein I was > supposed to flatten a list recursively. I am getting the solution properly > but wanted to know if I can optimize the code further. > > #!/usr/bin/env python > new_list=[] > def flatten(num_list): >

Re: [Tutor] Recursively flatten the list

2011-03-24 Thread Peter Otten
Dharmit Shah wrote: > I am learning Python and yesterday I cam across a definition wherein I was > supposed to flatten a list recursively. I am getting the solution properly > but wanted to know if I can optimize the code further. > > #!/usr/bin/env python > new_list=[] > def flatten(num_list): >

[Tutor] Recursively flatten the list

2011-03-23 Thread Dharmit Shah
Hello, I am learning Python and yesterday I cam across a definition wherein I was supposed to flatten a list recursively. I am getting the solution properly but wanted to know if I can optimize the code further. #!/usr/bin/env python new_list=[] def flatten(num_list): """ >>> flatten([2