Hi,
mydic = {'one': 1, 'two': 2}
# to access key:
for x in mydic.keys():
print x
# to access value --> use key
print mydic['one']
# 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 - <[EMAIL PROTECTED]> 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 - [email protected]
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
