[Tutor] help with slice
bob gailer
bgailer at alum.rpi.edu
Wed Mar 5 01:20:31 CET 2008
bob gailer wrote:
> 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...
>>
Call? Do you mean that these elements are callable objects (e.g.
functions) which you want to "call", or are you just trying to get a
list of the elements? My answer below addresses the 2nd meaning.
>> 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