[Tutor] Lists with just an item

Pujo Aji ajikoe at gmail.com
Mon Nov 21 10:38:45 CET 2005


Hi,
  mydic = {'one': 1, 'two': 2}

# to access key:
for x in mydic.keys():
print x

# to access value --> use key
print mydic['one']
 Cheers,
pujo


On 11/21/05, Negroup - <negroup at gmail.com> wrote:
>
> Hi all.
> In my application I have chosen as data structure a list of
> dictionaries. Each dictionary has just one key and the corresponding
> value.
>
> structure = [{'field1': lenght1}, {'field2': lenght2}, ........]
>
> Initially, to obtain the key and the value I used this code,
>
> for structure in record_structure:
> field_name = structure.keys()[0]
> displacement = structure[field_name]
>
> but after some thoughts I arrived to:
> field_name, displacement = structure.items()[0]
>
> which to me seems a lot better.
>
> At this point I don't like much that [0] to access a list that I know
> always contains a single element. Can I improve the code with
> something better?
>
> Thanks!
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051121/fb4f20da/attachment-0001.htm


More information about the Tutor mailing list