I have a list of dictionaries, and I would like to extract certain data based on certain conditions. I would like to extract only the currency (like int / float) if USD is displayed in the currency and more than 0.
curr = [{'currency': '6000.0000,EUR', 'name': 'Bob'},
{'currency': '0.0000,USD', 'name': 'Sara'},
{'currency': '2500.0000,USD', 'name': 'Kenny'},
{'currency': '0.0000,CND', 'name': 'Debbie'},
{'currency': '2800.0000,USD', 'name': 'Michael'},
{'currency': '1800.0000,CND', 'name': 'Aaron'},
{'currency': '2500.0000,EUR', 'name': 'Peter'}]
Results:
usd_curr = [2500.0000, 2800.0000]
Here is what I did.
usd_curr = [line for line in data if ',USD' in line['currency']]
usd_curr = [float(elem['currency'].split(',')[0]) for elem in curr if float(elem['currency'].split(',')[0]) > 0]
The list works, but actually my question is is there a better way to use a variable inside the list comprehension, so that it looks something like this:
usd_curr = [var = float(elem['currency'].split(',')[0]) for elem in curr if var > 0]