Cleaner idiom for text processing?

Paul Rubin http
Wed May 26 19:26:29 EDT 2004


mellis at frogwing.com (Michael Ellis) writes:
> for line in infile:
>    tokens = line.split()
>    dict = {}
>    for i in range(0, len(tokens),2) dict[tokens[i]] = tokens[i+1]
>    do_something_with_values(dict['foo'],dict['bar'])

Here's a pessimized version:

    for line in infile:
       tokens = line.split()
       d = {}
       while tokens:
          name = tokens.pop(0)
          value = tokens.pop(0)
          d[name] = value
       do_something_with_values(dict['foo'],dict['bar'])



More information about the Python-list mailing list