Displaying multiple results in a list

Philip Swartzleonard starx at pacbell.net
Tue Dec 4 01:40:04 EST 2001


Drew Fisher || Mon 03 Dec 2001 02:10:30p:

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

I think what the OP is looking for is something more along the lines of:

for each element in list[0:5]:
    	print element

Which is the next logical step from just making a newlist variable (sort of 
like making newlist then doing for each in newlist, without the 
intermdiate?), i think :)

-- 
Philip Sw "Starweaver" [rasx] :: <nothing>



More information about the Python-list mailing list