[Newbie]:Need help with "slicing"

Fredrik Lundh fredrik at pythonware.com
Thu Aug 26 06:59:36 EDT 1999


<gtnorton at my-deja.com> wrote:
>         Trying to figure out how to slice lists.  For example:
> 
>           >>> L = ['help', 'with', 'slice']
> 
>                        How do I slice to just show:
> 
>           >>>['help', 'slice']1st and last -with L[?:?]

a slice is everything *between* two indices.

to get the result you're after, you can either
*remove* a slice from your list:

del L[1:-1]
print L

or concatenate two slices (cheaper than you
might expect, since all Python's doing is copying
references around):

print L[:1] + L[-1:]

</F>





More information about the Python-list mailing list