[Tutor] help with slice

bob gailer bgailer at alum.rpi.edu
Wed Mar 5 01:13:44 CET 2008


washakie wrote:
> Could someone please explain 'slices' also for dictionaries?
>
> basically, I'd like to know how you would call every 3rd element in a list
> of lists... 
>
> My logic says: ThirdElems=List[:][2]
>
> Which to me reads, for every item in List (which are lists), return the
> third item.
>   
That is wishful reading. No basis in reality. List[:] gives you a 
shallow copy of List. [2] then refers to the 3rd element of that copy.

Try: [sublist[2] for sublist in List]

-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list