Simple exercise

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Mar 10 21:03:05 EST 2016


On 11/03/2016 01:45, BartC wrote:
> On 11/03/2016 01:21, Mark Lawrence wrote:
>> On 11/03/2016 00:05, BartC wrote:
>
>>> def last(a):
>>>      return a[-1]
>>>
>>> def init(a):                 # all except last element
>>>      return a[0:len(a)-1]
>>
>> What is wrong with a[0:1] ?
>
> The returns the head of the list. I need everything except the last
> element ('init' is from Haskell).

I missed out one character, it should of course have been:-

a[0:-1]

>
>>> for i in range(len(names)):
>>>      print (names[i],totals[i])
>>>
>>
>> Always a code smell when range() and len() are combined.
>
> Any other way of traversing two lists in parallel?
>

Use zip(), but as I suggested in my earlier reply there are better data 
structures than two lists in parallel for this problem.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list