Dictionaries: Difference between revisions
Mr. MacKenty (talk | contribs) (Created page with "right|frame|Programming basics<ref>http://www.flaticon.com/</ref> Dictionaries are sometimes found in other languages as “associative memories” or “...") |
Mr. MacKenty (talk | contribs) |
||
Line 7: | Line 7: | ||
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.<ref>https://docs.python.org/3.6/tutorial/datastructures.html</ref> | 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.<ref>https://docs.python.org/3.6/tutorial/datastructures.html</ref> | ||
== Creating a | == Creating a dictionary == | ||
<syntaxhighlight lang="python3"> | <syntaxhighlight lang="python3"> | ||
# The code below creates a list | # 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] ) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 05:36, 8 August 2017
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 list[edit]
# If you want to print a list (kind of ugly) you can simply:
print(polishAnimals)
# However, it is far more common to slice into a list
# The code below accesses the 2nd item in the list named 'polishAnimals'
print(polishAnimals[2])
# there is a lot more to slicing in Python.
Inserting into a list[edit]
# if we want to add onto a list (append) we could simply use the append method. The code below appends wild board onto the end of our list.
polishAnimals.append('wild boar')
# if we wanted to replace a certain element, we could simply overwrite it. Below we are replacing the 2nd item of our list with a new animal:
polishAnimals[1] = 'Stork'
Deleting an element from a list[edit]
# There are a few different ways to remove an element from a list. The first way is to call the remove method.
# The remove method works by finding the name of an element.
polishAnimals.remove('Moose')
# another way to remove an element from a list is to call the pop method. Pop-ing removes an index and return the value you removed.
# the line below removes the 2nd element from our list and makes it available for us to use.
polishAnimals.pop(1)
# we might use the pop method like this:
animalsDeleted = polishAnimals.pop[1]
# we can also use the del function.
del polishAnimals[2]
# to delete an entire list, call the clear method (this is only available in Python 3+
polishAnimals.clear
Do you understand this?[edit]
Standards[edit]
- Construct algorithms using pre- defined sub-programmes, one- dimensional arrays and/or collections.