Dictionaries: Difference between revisions
Mr. MacKenty (talk | contribs) |
Mr. MacKenty (talk | contribs) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
# Please note the KEY:VALUE construction | # Please note the KEY:VALUE construction | ||
studentEmail = {'Alice':'alice@google.com', 'Bob':'bob@facebook.com', 'Charlie':'charlez@microsoft.com', 'Daniel':' | studentEmail = { | ||
'Alice':'alice@google.com', | |||
'Bob':'bob@facebook.com', | |||
'Charlie':'charlez@microsoft.com', | |||
'Daniel':'danny@aswarsaw.org' | |||
} | |||
# Bob's email can be called by: | # Bob's email can be called by: | ||
Line 23: | Line 28: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Accessing a | == Accessing a dictionary == | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
# If you want to print a | # If you want to print a dictionary (kind of ugly) you can simply type: | ||
print( | print(studentEmail) | ||
# However, it is far more common to | # However, it is far more common to retrieve an element by it's key | ||
# The | # The instructions below accesses Bob's email address: | ||
print( | print(studentEmail['Bob']) | ||
# | # in addition, we have the following functions to access elements in a dictionary<ref>https://www.digitalocean.com/community/tutorials/understanding-dictionaries-in-python-3</ref>: | ||
# dict.keys() isolates keys | |||
# dict.values() isolates values | |||
# dict.items() returns items in a list format of (key, value) tuple pairs | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Inserting into a | == Inserting into a dictionary == | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
# if we want to add | # if we want to add into a dictionary we can simply type: | ||
studentEmail['Bill'] = 'bmackenty@aswarsaw.org' | |||
# if we wanted to replace a certain element, we could simply overwrite it. | # if we wanted to replace a certain element, we could simply overwrite it. | ||
studentEmail['Bill'] = 'bmackenty@gmail.com' | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Deleting an element from a | == Deleting an element from a dictionary == | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
# | # The del function deletes an element from a list: | ||
del studentEmail['Bill'] | |||
</syntaxhighlight> | |||
== Iterating through dictionaries and testing dictionaries == | |||
<syntaxhighlight lang="python3"> | |||
studentEmail = { | |||
'Alice':'alice@google.com', | |||
'Bob':'bob@facebook.com', | |||
'Charlie':'charlez@microsoft.com', | |||
'Daniel':'danny@aswarsaw.org' | |||
} | |||
# iterate through the keys: | |||
for i in studentEmail.keys(): | |||
print(i) | |||
# | # iterate through the values: | ||
for i in studentEmail.values(): | |||
print(i) | |||
# iterate through the items: | |||
for i in studentEmail.items(): | |||
print(i) | |||
# to | # to test if a key is in a dictionary | ||
if "Alice" in studentEmail: | |||
print("Alice is in the dictionary") | |||
else: | |||
print("Alice is not in the dictionary") | |||
# to test if a value is in a dictionary | |||
if "alice@google.com" in studentEmail.values(): | |||
print("alice@google.com is in the dictionary") | |||
else: | |||
print("alice@google.com is not in the dictionary") | |||
# if we want to get the key from a value, this is one way to do it. | |||
for key, value in studentEmail.items(): | |||
if value == "alice@google.com": | |||
print(key) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Standards == | == Standards == |
Latest revision as of 10:31, 30 September 2020
![](/images/f/f0/Arrows.png)
Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys.
It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.
The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.[2]
Creating a dictionary[edit]
# The code below creates a dictionary
# Please note the KEY:VALUE construction
studentEmail = {
'Alice':'alice@google.com',
'Bob':'bob@facebook.com',
'Charlie':'charlez@microsoft.com',
'Daniel':'danny@aswarsaw.org'
}
# Bob's email can be called by:
studentEmail['Bob']
# (in a list, we would need to do something like: studentEmail[1] )
Accessing a dictionary[edit]
# If you want to print a dictionary (kind of ugly) you can simply type:
print(studentEmail)
# However, it is far more common to retrieve an element by it's key
# The instructions below accesses Bob's email address:
print(studentEmail['Bob'])
# in addition, we have the following functions to access elements in a dictionary<ref>https://www.digitalocean.com/community/tutorials/understanding-dictionaries-in-python-3</ref>:
# dict.keys() isolates keys
# dict.values() isolates values
# dict.items() returns items in a list format of (key, value) tuple pairs
Inserting into a dictionary[edit]
# if we want to add into a dictionary we can simply type:
studentEmail['Bill'] = 'bmackenty@aswarsaw.org'
# if we wanted to replace a certain element, we could simply overwrite it.
studentEmail['Bill'] = 'bmackenty@gmail.com'
Deleting an element from a dictionary[edit]
# The del function deletes an element from a list:
del studentEmail['Bill']
Iterating through dictionaries and testing dictionaries[edit]
studentEmail = {
'Alice':'alice@google.com',
'Bob':'bob@facebook.com',
'Charlie':'charlez@microsoft.com',
'Daniel':'danny@aswarsaw.org'
}
# iterate through the keys:
for i in studentEmail.keys():
print(i)
# iterate through the values:
for i in studentEmail.values():
print(i)
# iterate through the items:
for i in studentEmail.items():
print(i)
# to test if a key is in a dictionary
if "Alice" in studentEmail:
print("Alice is in the dictionary")
else:
print("Alice is not in the dictionary")
# to test if a value is in a dictionary
if "alice@google.com" in studentEmail.values():
print("alice@google.com is in the dictionary")
else:
print("alice@google.com is not in the dictionary")
# if we want to get the key from a value, this is one way to do it.
for key, value in studentEmail.items():
if value == "alice@google.com":
print(key)
Standards[edit]
- Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.