A query about list

Dave Benjamin ramen at lackingtalent.com
Thu Oct 9 17:24:18 EDT 2003


In article <slrnbobkhq.io6.ramen at lackingtalent.com>, Dave Benjamin wrote:
> In article <pan.2003.10.09.21.16.17.434674 at softhome.net>, Santanu Chatterjee wrote:
>> I was trying something like a.insert(1,a.pop(1)) but I need to
>> modify a.pop(1) somehow so that the brackets vanish ...you know
>> what I mean. Is that possible ? 
> 
> Ahh, I see what you mean, now. You probably want slice assignment.
> 
> Try, for starters:
> a[1:2] = a[1]
> 
> You'll probably still need to use type().

This *seems* to work, but I have a sneaking feeling there's a bug in here as
a result of the slice assignment. I haven't been able to find a boundary
case that breaks this, but it seems like the slice assignment would mess up
the indexes in the loop. Can anyone find a way to either break or improve
this?

>>> a = [1, [2, 3, 4], 5, 6, 7, [8, 9, 10], 11, 12]
>>> def flatten_sublists(lst):
...     for i, item in enumerate(lst):
...         if type(item) is type([]):
...             lst[i:i+1] = lst[i]
...
>>> flatten_sublists(a)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

-- 
.:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g   l i f e   o u t   o f   t h e   c o n t a i n e r :




More information about the Python-list mailing list