[Tutor] Lists with just an item

Negroup - negroup at gmail.com
Mon Nov 21 11:26:59 CET 2005


2005/11/21, Brian van den Broek <broek at cc.umanitoba.ca>:

> Hi Negroup,
>
> why not just make structure a dict:
>
> structure = {'field1': lenght1, 'field2': lenght2}
>
> What does having a list of dicts all with a single key add but a layer
> of referencing when you want to access?
>
> If you are trying to preserve order and your key examples are real:
>
>  >>> structure = {'field1': 1, 'field2': 2, 'field3': 3}
>  >>> for item in sorted(structure.items()): print item

At first I used a unique dictionary, but as you told, the point is
that I need to preserve the order of items, so I switched to list of
dictionaries. I neither can rely on sorting 'field1', 'field2', ...,
as these are not real key names and instead vary from case to case
(they are obtained splitting in chunks the header line of a file
submitted by a user, that may be different each time).

So I'm going to think that my solution is at least "acceptable", wow ;-)

> Best,
>
> Brian vdB

Thanks


More information about the Tutor mailing list