how to extract columns like awk $1 $5

Carl Banks invalidemail at
Sat Jan 8 06:48:22 CET 2005

Roy Smith wrote:
> Hmmm.  There's something going on here I don't understand.  The ref
> manual (3.3.5 Emulating container types) says for __getitem__(),
> for loops expect that an IndexError will be raised for illegal
> to allow proper detection of the end of the sequence."  I expected my

> little demo class to therefore break for loops, but they seem to work

> fine:
> >>> import awk
> >>> l = awk.awkList ("foo bar baz".split())
> >>> l
> ['foo', 'bar', 'baz']
> >>> for i in l:
> ...     print i
> ...
> foo
> bar
> baz
> >>> l[5]
> ''
> Given that I've caught the IndexError, I'm not sure how that's

The title of that particular section is "Emulating container types",
which is not what you're doing, so it doesn't apply here.  For built-in
types, iterators are at work.  The list iterator probably doesn't even
call getitem, but accesses the items directly from the C structure.

More information about the Python-list mailing list