Key Ideas
- Keys
- To retrieve all keys in a dictionary named
inventory
:inventory.keys()
- To retrieve all keys as a list:
list(inventory.keys())
- To retrieve keys one at a time:
for key in inventory:
- To check if a key named
"kiwi"
exists:if "kiwi" in inventory:
- To retrieve all keys in a dictionary named
- Values
- To retrieve all values:
inventory.values()
- To retrieve a specific value, producing a runtime error if the key is not present:
inventory[key-name]
- To retrieve a specific value, producing NO runtime error if the key is not present:
inventory.get(key-name)
orinventory.get(key-name, value-to-return-if-key-not-present)
- To retrieve all values:
- Key-Value Pairs
- To retrieve all key-value pairs:
inventory.items()
- To retrieve all key-value pairs:
- Aliasing vs. Copying
- An example of an alias:
dictionary1 = dictionary2
- The expression
dictionary2 is dictionary1
#returns True - An example of a copy:
dictionary2 = dictionary1.copy()
- The expression
dictionary2 is dictionary1
#returns False
- An example of an alias:
In-Class Learning
- Edit ebird_problem.py so the program use the data in the
mt_recents
dictionary to create a csv file calledebird_recent.csv
. (Hint: you’ll probably want to uselist(mt_recents.keys()
) (see above in Key Ideas).
# ebird_problem.py
mt_recents = {
'speciesCode': 'houspa',
'comName': 'House Sparrow',
'sciName': 'Passer domesticus',
'locId': 'L17967461',
'locName': 'Casita Del Rio',
'obsDt': '2022-03-01 09:49',
'howMany': 10,
'lat': 47.101325,
'lng': -104.730941,
'obsValid': True,
'obsReviewed': False,
'locationPrivate': True,
}
# create header
labels = list(mt_recents.keys()) # create list of all keys
header = ", ".join(labels) # create csv string of labels
# create data line
data = [] # create empty list to store values
for item in labels: # iterate through list of keys
data += [str(mt_recents[item])] # append value to data list
data_line = ", ".join(data) # create csv string of data
# write to csv file
output = open("recent.csv", "w") # create file object
output.write(header + "\n") # write header to file
output.write(data_line + "\n") # write data to file
output.close()
Active Learning
- Write a program that allows the user to enter a string. It then prints a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times each letter occurs. Case should be ignored. A sample run of the program might look this this:
Please enter a sentence: ThiS is String with Upper and lower case Letters.
a 2
c 1
d 1
e 5
g 1
h 2
i 4
l 2
n 2
o 1
p 2
r 4
s 5
t 5
u 1
w 2
# ex1.py
import string
sentence = input("Please enter a sentence: ")
letters = {}
for character in sentence.lower():
if character in string.ascii_lowercase:
letters[character] = letters.get(character, 0) + 1
for character in sorted(letters.keys()):
print("{} {}".format(character, letters[character]))
- Give the Python interpreter’s response to each of the following from a continuous interpreter session:
>>> d = {'apples': 15, 'bananas': 35, 'grapes': 12}
>>> d['banana']
>>> d['oranges'] = 20
>>> len(d)
>>> 'grapes' in d
>>> d['pears']
>>> d.get('pears', 0)
>>> fruits = d.keys()
>>> fruits.sort()
>>> print(fruits)
>>> del d['apples']
>>> 'apples' in d