Determining the length of strings in a list

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Mar 13 18:13:33 EST 2005


robin.siebler at palmsource.com wrote:
> I have a dictionary.  Each key contains a list.  I am using the
> contents of the list to build a portion of a command line.
> 
> However, before I can build the command line, I have to make sure that
> the command isn't too long.  This means that I have to step through
> each item in the list twice: once to check the length and once to build
> the command.  Is there an easier/better way to do this?

I'm not exactly sure what is done in "break command portion into
multiple portions," but you could always build the command and then
check the list.

Or you can get the length of all of the values this way:

sum(len(value) for value in cmdline_dict.itervalues())

I don't think it's a good idea to call your dict "dict" as it shadows a
built-in.
-- 
Michael Hoffman



More information about the Python-list mailing list