FAQ
I understand that the keys in a dictionary are ordered not randomly but
something practically close to it, but if I create a SQL query like so:

query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()),
','.join(stats.values()))

Can I at least rely on the value being in the same index as its
corresponding key?

--
Wells Oliver
wells at submute.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090803/8a28386e/attachment.htm>

Search Discussions

  • Chris Rebert at Aug 3, 2009 at 9:25 pm

    On Mon, Aug 3, 2009 at 1:47 PM, Wells Oliverwrote:
    I understand that the keys in a dictionary are ordered not randomly but
    something practically close to it, but if I create a SQL query like so:

    query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()),
    ','.join(stats.values()))

    Can I at least rely on the value being in the same index as its
    corresponding key?
    Yes. Per http://docs.python.org/library/stdtypes.html#dict.items :
    """
    Note: Keys and values are listed in an arbitrary order which is
    non-random, varies across Python implementations, and depends on the
    dictionary?s history of insertions and deletions. If items(), keys(),
    values(), iteritems(), iterkeys(), and itervalues() are called with no
    intervening modifications to the dictionary, the lists will directly
    correspond. This allows the creation of (value, key) pairs using
    zip(): pairs = zip(d.values(), d.keys()).
    """

    Cheers,
    Chris
  • Gabriel Genellina at Aug 4, 2009 at 1:18 am
    En Mon, 03 Aug 2009 17:47:23 -0300, Wells Oliver <wells at submute.net>
    escribi?:
    I understand that the keys in a dictionary are ordered not randomly but
    something practically close to it, but if I create a SQL query like so:

    query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()),
    ','.join(stats.values()))

    Can I at least rely on the value being in the same index as its
    corresponding key?
    You already got the answer you wanted, but note that building a SQL
    statement that way is unsafe [1]. I prefer this way:

    query= 'INSERT INTO Batting (%s) VALUES(%s)' % (
    ','.join(stats.keys()),
    ','.join(['?']*len(stats)))
    cursor.execute(query, stats.values())

    [1] If you don't know what "SQL injection" means, see http://xkcd.com/327/

    --
    Gabriel Genellina
  • Benjamin Kaplan at Aug 4, 2009 at 2:15 am

    On Mon, Aug 3, 2009 at 9:18 PM, Gabriel Genellina wrote:

    [1] If you don't know what "SQL injection" means, see http://xkcd.com/327/
    I love how XKCD is one of the preferred learning tools (along with
    Wikipeida) for people on this list. I think Randall Munroe should make
    a comic about it. :)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedAug 3, '09 at 8:47p
activeAug 4, '09 at 2:15a
posts4
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase