Changes

Jump to: navigation, search

OPS435 Python Lab 4

217 bytes added, 14:08, 15 June 2017
PART 3 - Dictionaries
help(dict_york.keys)
dict_york.keys()
</source>Armed with this information, We can retrieve individual values from a dictionary by provide the key associated with the value<br><br>
::#For example, issue the following:<source>
dict_york['Address']
dict_york['Postal Code']
</source>
::#Dictionary keys can be any immutable values, such as: strings, numbers, and tuples. Trying adding a couple new keys and values to the dictionaryby issuing:<source>
dict_york['Country'] = 'Canada'
dict_york
dict_york.keys()
</source>
::#Study the output and then issue the following to add another key:value pair<source>
dict_york['Province'] = 'BC'
dict_york
dict_york.values()
dict_york.keys()
</source>::#Dictionary keys must be '''unique'''. Attempting to add a key that already exists in the dictionary will overwrite the existing value for that key::#To demonstrate, issue the following:<source>
dict_york['Province'] = 'ON'
dict_york
dict_york.keys()
</source>
::#These lists that contain the values and keys of the dictionary are not <u>real </u> python lists; instead, they are view views of the dictionary::#However we <br><br>On the other hand, you can change these from views into usable lists by using the '''list() ''' function, (where the index can be used to access individual values).<br><br>::#For example, issue the following:<source>
list_of_keys = list(dict_york.keys())
list_of_keys[0]
</source> ::#Lists In addition, lists can be changed into sets if we would like to perform comparisons with another set<br><br>::#Issue the following:<source>
set_of_keys = set(dict_york.keys())
set_of_values = set(dict_york.values())
13,420
edits

Navigation menu