<jupyter_start><jupyter_text>Beginning Programming with Python---------------------------------Session 3: Dictionaries======================================* Introduction* Methods* Iterating* Copying (Dictionary comprehensions)* Nested structures  * A list of dictionaries  * A dictionary of lists Introduction------------<jupyter_code>phoebe = 10
maru = 8
dogs = [10, 8]
print(dogs)
dogs = [phoebe, maru]
print(dogs)

dog_ages = [10, 8]
dog_names = ['phoebe', 'maru']
print(f"The dog {dog_names[0]} is {dog_ages[0]} years old.")
dogs = {'phoebe': 10, 'maru': 8}
print(dogs)
dogs.keys()
dogs.values()
dogs['phoebe']
dogs['maru']
dogs[8]
dog_i_want_to_see = 'spot'

# Style 1: Defensive programming
if dog_i_want_to_see in dogs:
    print(dogs[dog_i_want_to_see])
else:
    print(f"Sorry, {dog_i_want_to_see} isn't in my database")
# Style 2: Using Exceptions
try:
    print(dogs[dog_i_want_to_see])
except KeyError:
    print(f"Sorry, {dog_i_want_to_see} isn't in my database")
del dogs['maru']
print(dogs)<jupyter_output>{'phoebe': 10}<jupyter_text>Methods-------<jupyter_code>import this
dogs['maru']
help(dict)
print( dogs.get('maru') )
print( dogs.get('maru', 'Not found') )
dogs
dogs.items()
dogs.clear()
dogs
the_dog = dogs.get('maru')
if the_dog is not None:
    print(f"Found dog: {the_dog}")
else:
    print("Not found")<jupyter_output>Not found<jupyter_text>Iterating---------<jupyter_code>dogs = {'phoebe': 10, 'maru': 8, 'pebbles': 3}
for dog in dogs:
    print(f"{dog.title()} is {dogs[dog]} years old.")
dogs
dogs.items()
# Much nicer than #38 above.
for name, age in dogs.items():
    print(f"{name.title()} is {age} years old.")<jupyter_output>Phoebe is 10 years old.
Maru is 8 years old.
Pebbles is 3 years old.<jupyter_text>Copying-------<jupyter_code>numbers = list(range(20))
print(numbers)
numbers2 = [x * 100 for x in numbers if x % 2 == 0]
print(numbers2)
dogs
# The old way
dogs2 = {}
for name, age in dogs.items():
    dogs2[name.title()] = age
dogs2
# The "new" way: a dictionary comprehension
{name.title():age  for name, age in dogs.items()  if age < 5}
dogs
dogs.copy()
help(dogs.copy)
id(list(dogs.keys())[0])
id(list(dogs.copy().keys())[0])
list(dogs.copy().keys())[0]
dogs_copy = dogs.copy()
del dogs_copy['phoebe']
dogs_copy
dogs<jupyter_output><empty_output><jupyter_text>Nested Structures-----------------<jupyter_code>countries = {
    'usa': ['nyc', 'chicago', 'la'],
    'canada': ['ottawa', 'vancouver', 'calgary']
}
countries['usa']
countries['canada']
countries['canada'][0]
type(countries['canada'])
large_canadian_cities = countries['canada']
print(large_canadian_cities)
large_canadian_cities.append('Quebec City')
large_canadian_cities
large_canadian_cities
del large_canadian_cities[-1]
large_canadian_cities
countries
countries_copy = countries.copy()
del countries_copy['usa']
countries_copy
countries
large_canadian_cities
del large_canadian_cities[0]
large_canadian_cities
countries
countries_copy
# A very primitive Internationalization library
i18n = {
    'en': {
        'accept': 'Ok',
        'cancel': 'Cancel'
    },
    'es': {
        'accept': 'Ok'
    },
    'fr': {
        'accept': 'Ok'
    },
    'de': {
        'accept': 'Ok',
        'cancel': 'Abbrechen'
    }
}

# How would we use this?
lang = 'de'
print(i18n[lang]['accept'], i18n[lang].get('cancel', i18n['en']['cancel']))
books = []
books.append({'author': 'Smith', 'length in pages': 187, 'title': 'The Planet Mars'})
books.append({'author': 'Sobol', 'title': 'Encyclopedia Brown'})
print(books)
len(books)
for title in sorted([a_book['title'] for a_book in books]):
    print(title)
books
2 ** 4
2 ^ 4
sum([1,2,3])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.191500953418687, 'fraction_numerical': 0.018251157722691366, 'mean_word_length': 4.828571428571428, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '589477', 'license': 'MIT', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1394, 'n_words': 322, 'path': 'april-2019/3-dictionaries.ipynb', 'repo_name': 'dogweather/beginning-programming-with-python', 'size': '3671'}