# Is there an easier way to express this list slicing?

Thomas Ploch Thomas.Ploch at gmx.net
Thu Nov 30 21:34:11 CET 2006

```John Henry schrieb:
> Thomas Ploch wrote:
> <snip>
>> I had a little bit of fun while writing this:
>>
>> itemList = (a,b,c1,c2,c3,d1,d2,d3,d4,d5) and
>> itemList2 = (a1,a2,a3,b,c,d1,d2,d3,d4,d5) the next time.
>>
>
> Huh?  What's a,b,....d5?
>

John Henry schrieb:
> > Thomas Ploch wrote:
> > <snip>
>> >> I had a little bit of fun while writing this:
>> >>
>> >> itemList = (a,b,c1,c2,c3,d1,d2,d3,d4,d5) and
>> >> itemList2 = (a1,a2,a3,b,c,d1,d2,d3,d4,d5) the next time.
>> >>
> >
> > Huh?  What's a,b,....d5?
> >

>>>If I have a list of say, 10 elements and I need to slice it into
>>> irregular size list, I would have to create a bunch of temporary
>>> variables and then regroup them afterwords, like:
>>>
>>> # Just for illustration. Alist can be any existing 10 element list
>>> a_list=("",)*10
>>> (a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
>>> alist=(a,)
>>> blist=(b,)
>>> clist=(c1,c2,c3)
>>> dlist=(d2,d3,d4,d5)

>> def getSlices(aCount, bCount, cCount, dCount, items):
>>     a,b,c,d = (items[0:aCount],
>>                 items[aCount:aCount+bCount],
>>                 items[aCount+bCount:aCount+bCount+cCount],
>> 		item[aCount+bCount+cCount:aCount+bCount+cCount+dCount])
>
> You meant "items" here, right?
>
>>     return list(a),list(b),list(c),list(d)
>>
>>>>> a,b,c,d = getSlices(1,1,3,5,itemList)
>>>>> print a,b,c,d
>> ['a'] ['b'] ['c1', 'c2', 'c3'] ['d1', 'd2', 'd3', 'd4', 'd5']
>>
>>>>> a,b,c,d = getSlices(3,1,1,0,itemList2)
>>>>> print a,b,c,d
>> ['a1', 'a2', 'a3'] ['b'] ['c'] []
>>
>> %-)
>>
>> Thomas
>

```