Displaying multiple results in a list

Drew Fisher drew at level3.net
Mon Dec 3 17:10:30 EST 2001


> Wanted to know if there was a way to specify ranges in a list such as:
> 
> newlist = ["1", "2", "3", "4", "5", "6"]
> 
> print newlist[1..3]  #ie:  print newlist[1], newlist[2], newlist[3]
> 
> Can something like this be done?
> 
> /russ
> -- 

Sure.

Just use the ":" character to seperate the start / stop index:

newlist = [1,2,3,4,5,6,7]
print newlist [start:stop]

e.g.: 

>>> newlist = [1,2,3,4,5,6,7]
>>> print a[1:5]
[2, 3, 4, 5]

Hope this helps.
Drew





More information about the Python-list mailing list