In [12]:
kittens_data = []
kittens = document.find_all('div', {'class': 'kitten'})
for item in kittens:
    h2_tag = item.find('h2')
    a_tags = item.find_all('a')
    all_shows_str = []
    for a_tag_item in a_tags:
        tag_str = a_tag_item.string
        all_shows_str.append(tag_str)
    #(1) create a dictionary and add to it the relevant key/value pairs
    kitten_map = {}
    kitten_map['name'] = h2_tag.string
    kitten_map["tvshows"] = all_shows_str
    #(2) append that dictionary to the kittens data list
    string_with_all_show_names = ", ".join(all_shows_str)
    #print(h2_tag.string + ":", string_with_all_show_names)
    kittens_data.append(kitten_map)
print(kittens_data)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-c5c4b97510ff> in <module>()
      1 kittens_data = []
----> 2 kittens = document.find_all('div', {'class': 'kitten'})
      3 for item in kittens:
      4     h2_tag = item.find('h2')
      5     a_tags = item.find_all('a')

NameError: name 'document' is not defined

In [ ]:
len(kittens_data)

In [ ]:

THIRD ASIDE: MAKING DICTIONARIES


In [ ]:
#declaring a dictionary 
x = {'a': 1, 'b': 2, 'c': 3}

In [2]:
#get a value out of a dictionary:
x['a']


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-ccd0b25ef3a5> in <module>()
      1 #get a value out of a dictionary:
----> 2 x['a']

NameError: name 'x' is not defined

In [3]:
x.keys()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-f915c109abb8> in <module>()
----> 1 x.keys()

NameError: name 'x' is not defined

In [ ]:
for k in x.keys():
    print(k)
#will come out in a random order...thats normal

In [9]:
#target: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, ... 10: 100}
squares = {}
for n in range(1, 11):
    squares[n] = n* n
print(squares)


{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

In [7]:
squares[7]


Out[7]:
49

In [10]:
names = ["Aaron", "Bob", "Caroline", "Daphne"]
#target: {"Aaron": 5, "Bob": 3, "Caroline": 8, "Daphne": 6}
name_length_map = {}
for item in names:
    name_length_map[item] = len(item)
print(name_length_map)


{'Caroline': 8, 'Aaron': 5, 'Bob': 3, 'Daphne': 6}

In [ ]: