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
" 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
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
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
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):
>
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):
>
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