Python and the power of 'first class' everything


>>> field_types = [str, int, float]
>>> fields = [ty(val) for ty,val in zip(field_types,raw_fields)]
>>> fields
['ACME', 100, 123.45]

In this second example, if you want to convert data from a different file with different columns, you just make changes to field_types. All of the other code works without modification.


This tip is written by David Beazley, author of the Python Essential Reference, now available in a new Fourth Edition. It is published by Addison-Wesley Professional, as part of its Developer's Library (, Copyright 2009 by Pearson Education, Inc. ISBN 0672329786. To read a sample chapter, on Types and Objects, please visit the publisher site: or, Safari Books Online subscribers can access the entire book here:
Join us:






DevelopmentWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Ask a Question