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)
In [ ]:
len(kittens_data)
In [ ]:
In [ ]:
#declaring a dictionary
x = {'a': 1, 'b': 2, 'c': 3}
In [2]:
#get a value out of a dictionary:
x['a']
In [3]:
x.keys()
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)
In [7]:
squares[7]
Out[7]:
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)
In [ ]: