[Tutor] Iterating through a list of strings

Luke Paireepinart rabidpoobear at gmail.com
Mon May 3 12:25:50 CEST 2010


2010/5/3 spir ☣ <denis.spir at gmail.com>:
> On Mon, 03 May 2010 10:55:11 +0200
> Stefan Behnel <stefan_ml at behnel.de> wrote:
>
>> Luke Paireepinart, 03.05.2010 10:27:
>> > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote:
>> >>                 line = line.split('%', 1)[0]
>> >
>> > lines = [line[:line.index('%')] for line in ...
>>
>> Agreed that
>>
>>      line = line[:line.index('%')]
>>
>> is slightly more readable than
>>
>>      line = line.split('%', 1)[0]
>
> But, to my eyes,
>    endpos = line.index('%')
>    line = line[:endpos]
> is even more readable ;-)
>

I prefer this:
search_string_percent_sign = '%'
end_position_in_line = line.index(search_string_percent_sign)
temp = line[:end_position_in_line]
line = temp

-Luke


More information about the Tutor mailing list