Dictionaries are mappings of unique keys to a value. Note the unique there; they contain key-value mappings, but there is only ever one copy of a key.
This restriction gives the dictionary implementation its power; you can look up the value for any key in constant time. Regardless of how many (unique) keys you put into a dictionary, you can expect that in the common case it'll not take more time to look up any key than in a small dictionary.
To manage this feat, dictionaries do not care about the order the keys are given in; the implementation will put them in an order (internally) that is more convenient to the dictionary than it is to you. See Why is the order in Python dictionaries and sets arbitrary?
All this just means you misunderstood what dictionaries are for. You just want to extract the first elements of your list so you can pass them to a query:
queryparams = [l[0] for l in lists]
then give those to a pypyodbc SQL query using parameters:
query = 'SELECT tagalog FROM myDatabase WHERE english in ({})'.format(
', '.join(['?'] * len(queryparams)))
cursor.execute(query, queryparams)
for row in cursor:
print('Tagalog:', row[0])
I used a WHERE <column> IN (<value1>, <value2>, .., <valueN>)
query here to limit what Tagalog words should be looked up. To make that work with query parameters you need to generate a list of ?
placeholders first.
A IN
SQL membership test treats the elements as a set (unique values only again) so you may as well make queryparams
a set here and avoid sending duplicate words to the database:
queryparams = Iist({l[0] for l in lists})
The set is turned back into a list because I don't know if pypyodbc accepts sets as query parameter value input.
If you needed to use the input order to map English to Tagalog, use the database results as a dictionary:
query = 'SELECT english, tagalog FROM myDatabase WHERE english in ({})'.format(
', '.join(['?'] * len(queryparams)))
cursor.execute(query, queryparams)
english_to_tagalog = dict(cursor) # use each (english, tagalog) pair as a mapping
output = [english_to_tagalog[l[0]] for l in lists]
If your list of words gets very long, you may have to switch to using a temporary table, insert all your words in there (all of them, not just unique words) and use an inner join query to have SQL Server translate the words for you. You can have SQL Server preserve the order of your original input list that way too, so the final query result gives you Tagalog words in the same order.