Question regarding Python, Lists and Dictionaries

Status
Not open for further replies.
Hello,

So I've been spending the last week or so trying to learn Python. My background in programming: University introductory programming courses: Pascal (that's how old I am, haha) and assembly language.

Haven't really done much programming since then, but taught myself some basic Ruby and wrote some scripts. Now I'm trying to learn Python.

I've created a list of dictionaries, we'll call this master_data:
[{u'Client Name': u'John Smith', u'Color': u'Blue', u'Account Name': u'John Smith Chevy ', u'Account Number': u'Z10001'}, {u'Client Name': u'Jane Doe', u'Color': u'Red', u'Account Name': u'Jane Doe Toyota', u'Account Number': u'Z10056'}, {u'Client Name': u'Joe Plumber', u'Color': u'Flint Mica', u'Account Name': u'Joe Plumber Dodge', u'Account Number': u'Z10043'}, {u'Client Name': u'Sally Spell', u'Color': u'Hot Pink', u'Account Name': u'Sally Spell Corvette', u'Account Number': u'Z20089'}]

And I have a list of account numbers, we'll call this account_no.
[u'Z20089', u'Z10005', u'Z10001', u'Z10043']

What I want to do is check if any of the account numbers in my list of account numbers (account_no) exist in master_data. When checking if data exists between lists I understand I can do something like:

Code:
for k in list_1:
    if k not in list_2:
       print "account is not in list_2"

But I'm stuck figuring out the code when trying to check within dictionary located in a list (e.g. a 2D array).

Any pointers?

Thanks!
 
In Python, a dict of dicts may be a more natural fit.

>>> accounts = dict()
>>> accounts['Z10231'] = {'Client Name': 'Tommy Pajamas'}
>>> accounts['Z10231']['Color'] = 'Surf Blue'
>>> accounts['Z10231']
{'Client Name': 'Tommy Pajamas', 'Color': 'Surf Blue'}

Access the outer dict by your ID, and you can fetch a list of IDs with accounts.keys().

Disclaimer: I haven't used Python heavily for some time.
 
Status
Not open for further replies.