I have this code block that assigns an account type based on a list of acccount names. But it fails if an account name is not in any of the 'name' arrays.
How do I add a "default" type if not found? Or is there a more elegant way to do this?
accts = [
{'id': 1425396, 'name': 'Banana'},
{'id': 1425399, 'name': 'Schwab Brokerage'},
{'id': 1425400, 'name': 'Schwab'},
{'id': 1425411, 'name': 'CapitalOne'},
{'id': 1425428, 'name': '401K'},
{'id': 1425424, 'name': 'Venmo'},
{'id': 1425428, 'name': 'Geico'},
{'id': 1425428, 'name': 'PayPal'},
{'id': 1426349, 'name': 'Coinbase'},
{'id': 1426349, 'name': 'XXX'}
]
for acct in accts: acct['acct_type'] = next(acct_type for acct_type in
[
{'acct_type':'checking', 'accts':['Schwab','Venmo']},
{'acct_type':'credit', 'accts':['Banana','CapitalOne']},
{'acct_type':'other', 'accts':['Geico','PayPal']},
{'acct_type':'invest', 'accts':['Schwab Brokerage','401K','Coinbase']}
]
if acct['name'] in acct_type['accts'])['acct_type']
The last (XXX) account causes this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
help("keywords")) And what do you mean 'minimal reproducible code'?typeit is not a reserved word but builtin function and what I wanted say is to give some code that can be copied and reproduced easily