# create an empty dictionary 
empty_dict = {}
empty_dict = dict()

# create a dictionary with content
family = {'dad':'homer', 'mom':'marge', 'size':6}
faimly = dict(dad='homer', mom='marge', size=6)
family

# convert a liist of tuples into a dictionary
list_of_tuples = [('dad','homer'), ('mom','marge'), ('size',6)]
family=dict(list_of_tuples)
family

# creating a dictionary from a sequence of keys (no values created)
seq = ('name', 'age', 'sex')
d = dict.fromkeys(seq)
d

# creating a dictionary from a sequence of keys and a value to use as the default value
d = dict.fromkeys(seq, 10)
d

"""
Explanation: Python Dictionaries Reference
Table Of Contents

<a href="#1.-Construction">Construction</a>
<a href="#2.-Getting-Data">Getting Data</a>
<a href="#3.-Safer-Access-With-Get">Safer Access With Get</a>
<a href="#4.-Adding-and-Changing-Data">Adding and Changing Data</a>
<a href="#5.-String-Substitution-With-a-Dictionary">String Substitution With a  Dictionary</a>
<a href="#6.-Default-Elements">Default Elements</a>
<a href="#7.-Set-Operations-On-Keys-and-Items">Set Operations On Keys and Items</a>
<a href="#8.-Dictionary-Comprehensions">Dictionary Comprehensions</a>

1. Construction
End of explanation
"""


# pass a key ti return its value
family['dad']

# return number of key-value pairs
len(family)

# check if key exists in dictionary
'mom' in family

# only keys, not values, are checked
'marge' in family

# returns an iterable view of keys
family.keys()

# returns an iterable view of values
family.values()

# returns an iterable view of key-value pairs
family.items()

"""
Explanation: 2. Getting Data
End of explanation
"""


# equivalent to a dictionary lookup
family.get('mom')

# this would throw an error since the key does not exist
# family['grandma']

# return None if not found
family.get('grandma')

# provide a default return value if not found
family.get('grandma', 'not found')

# accesss an element within a list inside a dictionary
family['kids'][0]

"""
Explanation: 3. Safer Access With Get
End of explanation
"""


# edit an existing entry
family['cat'] = 'snowball'
family

# edit an existing entry
family['cat'] = 'snowball ii'
family

# delete an entry
del family['cat']
family

# dictionary value can be a list
family['kids']=['bart','lisa']
family

# remove an entry and return the value
family.pop('dad')

# add multiple-entries
family.update({'baby':'maggie', 'mom':'Marge', 'grandpa':'abe'})
family

family['kids'].remove('lisa')
family

"""
Explanation: 4. Adding and Changing Data
End of explanation
"""


# replaces %(baby)s with the result of looking up 'baby' in family
'youngest child is %(baby)s' % family

"""
Explanation: 5. String Substitution With a  Dictionary
End of explanation
"""


# using defaultdict 
from collections import defaultdict

d=defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(3)
d

# using setdefault
d={}
d.setdefault('a',[]).append(1)
d.setdefault('a',[]).append(2)
d.setdefault('b',[]).append(3)
d

"""
Explanation: 6. Default Elements
Ways to get a default element add to the dictionary on first access
End of explanation
"""


a = {
'x' : 1,
'y' : 2,
'z' : 3
}

b = {
'w' : 10,
'x' : 11,
'y' : 2
}

# Find keys in common
a.keys() & b.keys() # { 'x', 'y' }

# Find keys in a that are not in b
a.keys() - b.keys() # { 'z' }

# Find (key,value) pairs in common
a.items() & b.items() # { ('y', 2) }

"""
Explanation: 7. Set Operations On Keys and Items
End of explanation
"""


{key:a[key] for key in a.keys() - {'z', 'w'}}

"""
Explanation: These kinds of operations can also be used to alter or filter dictionary contents. For
example, suppose you want to make a new dictionary with selected keys removed. Here
is some sample code using a dictionary comprehension:
End of explanation
"""


prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}

# Make a dictionary of all prices over 200
{ key:value for key, value in prices.items() if value > 200 }

# Make a dictionary of tech stocks
tech_names = { 'AAPL', 'IBM', 'HPQ', 'MSFT' }
{ key:value for key,value in prices.items() if key in tech_names }

"""
Explanation: 8. Dictionary Comprehensions
End of explanation
"""


