keys and values lining up

Nathan Clegg nathan at islanddata.com
Tue Sep 28 12:07:20 EDT 1999


d.items() is my backup plan, of course, but I'd rather not.  My purpose is
to insert into a database based on the contents of a dictionary.  SQL
requires a list of keys and a separate list of values:

INSERT INTO table (key1, key2, key3) VALUES (val1, val2, val3)

If I were to use d.items(), I would them have to go back and use the list
of two-tuples to generate a two-tuple of lists :)  No point in going
through the back door if you can work it right.  If I'm guaranteed to get
keys and values in the same order from the two function calls, that's far
easier for my purposes.



On 28-Sep-99 Tim Peters wrote:
> [Nathan Clegg]
>> If d is a dict, am I guaranteed that d.keys() and d.values() will
>> line up, in proper order, if called successively with no modification
>> in between?  That is, is the following safe?
>>
>> k,v = d.keys(), d.values()
> 
> Yes, under the mild assumption that you don't have another thread
> running
> that manages to sneak in and mutate d between the calls.  If that's a
> potential problem, do
> 
>     kv = d.items()  # list of (key, value) tuples
> 
> If you want both keys and values, it's clearer to use the method
> designed
> for that purpose anyway.
> 
> something-fishy-about-keys-that-don't-unlock-anything-ly y'rs  - tim
> 
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list



----------------------------------
Nathan Clegg
 nathan at islanddata.com






More information about the Python-list mailing list