Created
April 10, 2015 14:06
-
-
Save Averroes/05ede0219be50b048086 to your computer and use it in GitHub Desktop.
extracting a subset of a dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of extracting a subset from a dictionary | |
from pprint import pprint | |
prices = { | |
'ACME': 45.23, | |
'AAPL': 612.78, | |
'IBM': 205.55, | |
'HPQ': 37.20, | |
'FB': 10.75 | |
} | |
# Make a dictionary of all prices over 200 | |
p1 = { key:value for key, value in prices.items() if value > 200 } | |
print("All prices over 200") | |
pprint(p1) | |
# Make a dictionary of tech stocks | |
tech_names = { 'AAPL', 'IBM', 'HPQ', 'MSFT' } | |
p2 = { key:value for key,value in prices.items() if key in tech_names } | |
print("All techs") | |
pprint(p2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment