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

Discussion in 'Python' started by John Henry, Nov 30, 2006.

1. ### John HenryGuest

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)

That obviously work but do I *really* have to do that?

BTW: I know you can do:
alist=a_list[0]
blist=a_list[1]
clist=a_list[2:5]
dlist=a_list[5:]

but I don't see that it's any better.

Can I say something to the effect of:

(a,b,c[0:2],d[0:5])=a_list # Obviously this won't work

??

I am asking this because I have a section of code that contains *lots*
of things like this. It makes the code very unreadable.

Thanks,

John Henry, Nov 30, 2006

2. ### Thomas PlochGuest

John Henry schrieb:
> 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)
>
> That obviously work but do I *really* have to do that?
>
> BTW: I know you can do:
> alist=a_list[0]
> blist=a_list[1]
> clist=a_list[2:5]
> dlist=a_list[5:]
>
> but I don't see that it's any better.
>
> Can I say something to the effect of:
>
> (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work
>
> ??
>
> I am asking this because I have a section of code that contains *lots*
> of things like this. It makes the code very unreadable.
>
> Thanks,
>

Nothing in your code actually __is__ a list. they are all tuples...
A list is:
aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]

Thomas

Thomas Ploch, Nov 30, 2006

3. ### John HenryGuest

Well, pardoon me.

Next.

Thomas Ploch wrote:
> John Henry schrieb:
> > 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)
> >
> > That obviously work but do I *really* have to do that?
> >
> > BTW: I know you can do:
> > alist=a_list[0]
> > blist=a_list[1]
> > clist=a_list[2:5]
> > dlist=a_list[5:]
> >
> > but I don't see that it's any better.
> >
> > Can I say something to the effect of:
> >
> > (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work
> >
> > ??
> >
> > I am asking this because I have a section of code that contains *lots*
> > of things like this. It makes the code very unreadable.
> >
> > Thanks,
> >

>
> Nothing in your code actually __is__ a list. they are all tuples...
> A list is:
> aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]
>
> Thomas

John Henry, Nov 30, 2006
4. ### Chris MellonGuest

On 11/30/06, Thomas Ploch <> wrote:
> John Henry schrieb:
> > 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)
> >
> > That obviously work but do I *really* have to do that?
> >
> > BTW: I know you can do:
> > alist=a_list[0]
> > blist=a_list[1]
> > clist=a_list[2:5]
> > dlist=a_list[5:]
> >
> > but I don't see that it's any better.
> >
> > Can I say something to the effect of:
> >
> > (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work
> >
> > ??
> >
> > I am asking this because I have a section of code that contains *lots*
> > of things like this. It makes the code very unreadable.
> >
> > Thanks,
> >

>
> Nothing in your code actually __is__ a list. they are all tuples...
> A list is:
> aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]
>

True but not relevant, really, he should have said "sequence". But
more importantly,
you don't show what you do with alist, blist,clist,dlist. Without
knowing what the end result is, nobody is going to be able to help you
eliminate the middle steps.

> Thomas
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Chris Mellon, Nov 30, 2006
5. ### Guest

John Henry wrote:
> Can I say something to the effect of:
>
> (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work

x = [...some list...]
a,b,c,d = x[:1],x[1:2],x[2:5],x[5:]

> I am asking this because I have a section of code that contains *lots*
> of things like this. It makes the code very unreadable.

Of course, if you're always slicing up lists the same way (say, into
1,1,3,5 element sections) then you could improve readability by writing
a function that takes the list and returns a tuple of the pieces, such
as:

def slice_list(x):
return x[:1],x[1:2],x[2:5],x[5:]

a,b,c,d = slice_list(first_list)
e,f,g,h = slice_list(second_list)

-Matt

, Nov 30, 2006
6. ### Neil CeruttiGuest

On 2006-11-30, John Henry <> wrote:
> 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)
>
> That obviously work but do I *really* have to do that?

Please post actual code we can run, rather than text that is
almost, but not quite, entirely unlike Python code.

> BTW: I know you can do:
> alist=a_list[0]
> blist=a_list[1]

Note that alist and blist are not necessarily lists, as you did
not use slice notation.

> clist=a_list[2:5]
> dlist=a_list[5:]
>
> but I don't see that it's any better.

I think it looks much better, personally.

If you are iterating through that sequence of slices a lot,
consider using a generator that yields the sequence.

>>> def parts(items):

... yield items[0:1]
... yield items[1:2]
... yield items[2:5]
... yield items[5:]

>>> for seq in parts(range(10)):

... print seq
[0]
[1]
[2, 3, 4]
[5, 6, 7, 8, 9]

--
Neil Cerutti
I guess there are some operas I can tolerate and Italian isn't one of them.
--Music Lit Essay

Neil Cerutti, Nov 30, 2006
7. ### Neil CeruttiGuest

On 2006-11-30, Neil Cerutti <> wrote:
> On 2006-11-30, John Henry <> wrote:
>> 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)
>>
>> That obviously work but do I *really* have to do that?

>
> Please post actual code we can run, rather than text that is
> almost, but not quite, entirely unlike Python code.

Ummm... that comment is withdrawn. :-O

--
Neil Cerutti

Neil Cerutti, Nov 30, 2006
8. ### Paul McGuireGuest

"John Henry" <> wrote in message
news:...
> 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)
>
> That obviously work but do I *really* have to do that?
>
> BTW: I know you can do:
> alist=a_list[0]
> blist=a_list[1]
> clist=a_list[2:5]
> dlist=a_list[5:]
>
> but I don't see that it's any better.

The slicing notation is about the best general solution.

If you are doing this a lot, you should write some sort of "break up the
list function". Here's one that takes a list of list lengths to break the
list into.

-- Paul

def splitUp(src,lens):
ret = []
cur = 0
for var,length in varmap:
if length is not None:
ret.append( a_list[cur:cur+length] )
cur += length
else:
ret.append( a_list[cur:] )
return ret

origlist = list("ABCDEFGHIJ")
alist, blist, clist, dlist = splitUp( origlist, (1,1,3,None) )
print alist, blist, clist, dlist

Prints
['A'] ['B'] ['C', 'D', 'E'] ['F', 'G', 'H', 'I', 'J']

Paul McGuire, Nov 30, 2006
9. ### Paul McGuireGuest

"Paul McGuire" <._bogus_.com> wrote in message
news:MmGbh.10434\$...
> "John Henry" <> wrote in message
> news:...

snip

Grrrr... that's what I get for not keeping editor and interpreter windows in
sync. My post was referencing vars I had defined in the interpreter, but
which the function had no clue of. !!! Here's a working version.

-- Paul

def splitUp(src,lens):
ret = []
cur = 0
for length in lens:
if length is not None:
ret.append( src[cur:cur+length] )
cur += length
else:
ret.append( src[cur:] )
return ret

origlist = list("ABCDEFGHIJ")
alist, blist, clist, dlist = splitUp( origlist, (1,1,3,None) )
print alist, blist, clist, dlist

Paul McGuire, Nov 30, 2006
10. ### John HenryGuest

wrote:
> John Henry wrote:
> > Can I say something to the effect of:
> >
> > (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work

>
> Your best bet is probably:
>
> x = [...some list...]
> a,b,c,d = x[:1],x[1:2],x[2:5],x[5:]
>

Dude! Why didn't I think of that (tunnel vision).

Thanks,

> > I am asking this because I have a section of code that contains *lots*
> > of things like this. It makes the code very unreadable.

>
> Of course, if you're always slicing up lists the same way (say, into
> 1,1,3,5 element sections) then you could improve readability by writing
> a function that takes the list and returns a tuple of the pieces, such
> as:
>
> def slice_list(x):
> return x[:1],x[1:2],x[2:5],x[5:]
>
> a,b,c,d = slice_list(first_list)
> e,f,g,h = slice_list(second_list)
>
> -Matt

John Henry, Nov 30, 2006
11. ### John HenryGuest

Paul McGuire wrote:
> "Paul McGuire" <._bogus_.com> wrote in message
> news:MmGbh.10434\$...
> > "John Henry" <> wrote in message
> > news:...

> snip
>
> Grrrr... that's what I get for not keeping editor and interpreter windows in
> sync. My post was referencing vars I had defined in the interpreter, but
> which the function had no clue of. !!! Here's a working version.
>
> -- Paul
>
>
> def splitUp(src,lens):
> ret = []
> cur = 0
> for length in lens:
> if length is not None:
> ret.append( src[cur:cur+length] )
> cur += length
> else:
> ret.append( src[cur:] )
> return ret
>
> origlist = list("ABCDEFGHIJ")
> alist, blist, clist, dlist = splitUp( origlist, (1,1,3,None) )
> print alist, blist, clist, dlist

Nice.

While we are at it, why not:

class splitUp(object):
def __init__(self,src):
self._src=list(src)
def slice(self, lens):
ret = []
cur = 0
for length in lens:
if length is not None:
ret.append( self._src[cur:cur+length] )
cur += length
else:
ret.append( self._src[cur:] )
return ret

alist, blist, clist, dlist = splitUp("ABCDEFGHIJ").slice((1,1,3,None))
print alist, blist, clist, dlist

John Henry, Nov 30, 2006
12. ### John HenryGuest

John Henry wrote:
> Paul McGuire wrote:
> > "Paul McGuire" <._bogus_.com> wrote in message
> > news:MmGbh.10434\$...
> > > "John Henry" <> wrote in message
> > > news:...

> > snip
> >
> > Grrrr... that's what I get for not keeping editor and interpreter windows in
> > sync. My post was referencing vars I had defined in the interpreter, but
> > which the function had no clue of. !!! Here's a working version.
> >
> > -- Paul
> >
> >
> > def splitUp(src,lens):
> > ret = []
> > cur = 0
> > for length in lens:
> > if length is not None:
> > ret.append( src[cur:cur+length] )
> > cur += length
> > else:
> > ret.append( src[cur:] )
> > return ret
> >
> > origlist = list("ABCDEFGHIJ")
> > alist, blist, clist, dlist = splitUp( origlist, (1,1,3,None) )
> > print alist, blist, clist, dlist

>
>
> Nice.
>
> While we are at it, why not:
>
> class splitUp(object):
> def __init__(self,src):
> self._src=list(src)
> def slice(self, lens):
> ret = []
> cur = 0
> for length in lens:
> if length is not None:
> ret.append( self._src[cur:cur+length] )
> cur += length
> else:
> ret.append( self._src[cur:] )
> return ret
>
> alist, blist, clist, dlist = splitUp("ABCDEFGHIJ").slice((1,1,3,None))
> print alist, blist, clist, dlist
>

Further, if splitUp is a sub-class of string, then I can do:

alist, blist, clist, dlist = "ABCDEFGHIJ".slice((1,1,3,None))

Now, can I override the slice operator?

John Henry, Nov 30, 2006
13. ### Thomas PlochGuest

John Henry schrieb:
> 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)
>
> That obviously work but do I *really* have to do that?
>
> BTW: I know you can do:
> alist=a_list[0]
> blist=a_list[1]
> clist=a_list[2:5]
> dlist=a_list[5:]
>
> but I don't see that it's any better.
>
> Can I say something to the effect of:
>
> (a,b,c[0:2],d[0:5])=a_list # Obviously this won't work
>
> ??
>
> I am asking this because I have a section of code that contains *lots*
> of things like this. It makes the code very unreadable.
>
> Thanks,
>

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.

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])
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

Thomas Ploch, Nov 30, 2006
14. ### John HenryGuest

John Henry wrote:

>
> Further, if splitUp is a sub-class of string, then I can do:
>
> alist, blist, clist, dlist = "ABCDEFGHIJ".slice((1,1,3,None))
>
> Now, can I override the slice operator?

Maybe like:

alist, blist, clist, dlist = newStr("ABCDEFGHIJ")[1,1,3,None]

where newStr is a sub-class of str, with a __repr__ that takes a
variable list of arguments?

(No clue how to code that yet, still pretty new to this)

Maybe they should make this a standard slicing feature....

John Henry, Nov 30, 2006
15. ### John HenryGuest

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?

> 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

John Henry, Nov 30, 2006
16. ### Thomas PlochGuest

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

>

Thomas Ploch, Nov 30, 2006
17. ### John HenryGuest

Thomas Ploch wrote:
>
> 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?
> > >

>
>

Oh, sorry.

John Henry, Nov 30, 2006
18. ### Paul McGuireGuest

"John Henry" <> wrote in message
news:...
>
> John Henry wrote:
>
>>
>> Further, if splitUp is a sub-class of string, then I can do:
>>
>> alist, blist, clist, dlist = "ABCDEFGHIJ".slice((1,1,3,None))
>>
>> Now, can I override the slice operator?

>
> Maybe like:
>
> alist, blist, clist, dlist = newStr("ABCDEFGHIJ")[1,1,3,None]
>

No need to contort string, just expand on your earlier idea. I changed your
class name to SplitUp to more more conventional (class names are usually
capitalized), and changed slice to __call__. Then I changed the lens arg to
*lens - note the difference in the calling format. Pretty close to what you
have above. Also, reconsider whether you want the __init__ function
list-ifying the input src - let the caller decide what to send in.

-- Paul

class SplitUp(object):
def __init__(self,src):
self._src=list(src)
def __call__(self, *lens):
ret = []
cur = 0
for length in lens:
if length is not None:
ret.append( self._src[cur:cur+length] )
cur += length
else:
ret.append( self._src[cur:] )
return ret

alist, blist, clist, dlist = SplitUp("ABCDEFGHIJ")(1,1,3,None)
print alist, blist, clist, dlist

Prints:
['A'] ['B'] ['C', 'D', 'E'] ['F', 'G', 'H', 'I', 'J']

Paul McGuire, Nov 30, 2006
19. ### John HenryGuest

Paul McGuire wrote:
> "John Henry" <> wrote in message
> news:...
> >
> > John Henry wrote:
> >
> >>
> >> Further, if splitUp is a sub-class of string, then I can do:
> >>
> >> alist, blist, clist, dlist = "ABCDEFGHIJ".slice((1,1,3,None))
> >>
> >> Now, can I override the slice operator?

> >
> > Maybe like:
> >
> > alist, blist, clist, dlist = newStr("ABCDEFGHIJ")[1,1,3,None]
> >

>
> No need to contort string, just expand on your earlier idea. I changed your
> class name to SplitUp to more more conventional (class names are usually
> capitalized), and changed slice to __call__. Then I changed the lens arg to
> *lens - note the difference in the calling format. Pretty close to what you
> have above. Also, reconsider whether you want the __init__ function
> list-ifying the input src - let the caller decide what to send in.
>

In fact, should be possible to make that any object the caller want to
send in...

> -- Paul
>
> class SplitUp(object):
> def __init__(self,src):
> self._src=list(src)
> def __call__(self, *lens):
> ret = []
> cur = 0
> for length in lens:
> if length is not None:
> ret.append( self._src[cur:cur+length] )
> cur += length
> else:
> ret.append( self._src[cur:] )
> return ret
>
> alist, blist, clist, dlist = SplitUp("ABCDEFGHIJ")(1,1,3,None)
> print alist, blist, clist, dlist
>
> Prints:
> ['A'] ['B'] ['C', 'D', 'E'] ['F', 'G', 'H', 'I', 'J']

Thanks for the help,

John Henry, Nov 30, 2006
20. ### Dennis Lee BieberGuest

On 30 Nov 2006 10:58:45 -0800, "John Henry" <>
declaimed the following in comp.lang.python:

> # 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)
>

These are tuples, not lists!
--
Wulfraed Dennis Lee Bieber KD6MOG

HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: )
HTTP://www.bestiaria.com/

Dennis Lee Bieber, Dec 1, 2006