In this part of the Python programming tutorial, we will cover Python dictionaries in more detail.
Python dictionary is a container of key-value pairs. It is mutable and can contain mixed types. A dictionary is an unordered collection. Python dictionaries are called associative arrays or hash tables in other languages. The keys in a dictionary must be immutable objects like strings or numbers. They must also be unique within a dictionary.
The next example will present two dictionary methods. The
The next code example will show, how to add two Python dictionaries.
Now we will show, how to remove a pair from a dictionary.
Programmers might want to sort the data in a normal or reverse order. They could sort the data by keys or by values.
More efficient sorting can be done with the built-in
In the next example, we are going to sort the items by their values.
A view is a virtual read-only container. A view does not make a copy of a dictionary.
In the next example we show that a view reflects dictionary changes.
In this part of the Python tutorial, we have written about Python dictionaries.
Python dictionary is a container of key-value pairs. It is mutable and can contain mixed types. A dictionary is an unordered collection. Python dictionaries are called associative arrays or hash tables in other languages. The keys in a dictionary must be immutable objects like strings or numbers. They must also be unique within a dictionary.
Creating dictionaries
First, we will show how to create Python dictionaries.#!/usr/bin/pythonIn the example, we create four dictionaries. In four different ways. Later we print the contents of these dictionaries to the console.
weekend = { "Sun": "Sunday", "Mon": "Monday" }
vals = dict(one=1, two=2)
capitals = {}
capitals["svk"] = "Bratislava"
capitals["deu"] = "Berlin"
capitals["dnk"] = "Copenhagen"
d = { i: object() for i in range(4) }
print weekend
print vals
print capitals
print d
weekend = { "Sun": "Sunday", "Mon": "Monday" }We create a weekend dictionary using dictionary literal notation. The key-value pairs are enclosed by curly brackets. The pairs are separated by commas. The first value of a pair is a key, which is followed by a colon character and a value. The "Sun" string is a key and the "Sunday" string is a value.
vals = dict(one=1, two=2)Dictionaries can be created using the
dict()
function. capitals = {}This is the third way. An empty capitals dictionary is created. Three pairs are added to the dictionary. The keys are inside the square brackets, the values are located on the right side of the assignment.
capitals["svk"] = "Bratislava"
capitals["deu"] = "Berlin"
capitals["dnk"] = "Copenhagen"
d = { i: object() for i in range(4) }A dictionary is created using a dictionary comprehension. The comprehension has two parts. The first part is the
i: object()
expression, which is executed for each cycle of a loop. The second part is the for i in range(4)
loop. The dictionary comprehension creates a dictionary having four pairs, where the keys are numbers 0, 1, 2, 3 and the values are simple objects. $ ./create_dict.py
{'Sun': 'Sunday', 'Mon': 'Monday'}
{'two': 2, 'one': 1}
{'svk': 'Bratislava', 'dnk': 'Copenhagen', 'deu': 'Berlin'}
{0: <object object at 0xb76cb4a8>, 1: <object object at 0xb76cb4b0>,
2: <object object at 0xb76cb4b8>, 3: <object object at 0xb76cb4c0>}
Basic operations
The following examples will show some basic operations with Python dictionaries.#!/usr/bin/pythonWe have a basket with different fruits. We perform some operations on the basket dictionary.
basket = { 'oranges': 12, 'pears': 5, 'apples': 4 }
basket['bananas'] = 5
print basket
print "There are %d various items in the basket" % len(basket)
print basket['apples']
basket['apples'] = 8
print basket['apples']
print basket.get('oranges', 'undefined')
print basket.get('cherries', 'undefined')
basket = { 'oranges': 12, 'pears': 5, 'apples': 4 }The basket dictionary is created. It has initially three key-value pairs.
basket['bananas'] = 5A new pair is created. The 'bananas' string is a key, the 5 integer is the value.
print "There are %d various items in the basket" % len(basket)The
len()
function gives the number of pairs in the dictionary. print basket['apples']The value of the 'apples' key is printed to the terminal.
basket['apples'] = 8The value of the 'apples' key is modified. It is set to number 8.
print basket.get('oranges', 'undefined')The
get()
method retrieves the value of a specified key. If there is no such a key, the second parameter of the method is returned. print basket.get('cherries', 'undefined')This line returns 'undefined'. There are no cherries in the basket.
$ ./basics.pyExample output.
{'bananas': 5, 'pears': 5, 'oranges': 12, 'apples': 4}
There are 4 various items in the basket
4
8
12
undefined
The next example will present two dictionary methods. The
fromkeys()
and the setdefault()
method. #!/usr/bin/pythonThe
basket = ('oranges', 'pears', 'apples', 'bananas')
fruits = {}.fromkeys(basket, 0)
print fruits
fruits['oranges'] = 12
fruits['pears'] = 8
fruits['apples'] = 4
print fruits.setdefault('oranges', 11)
print fruits.setdefault('kiwis', 11)
print fruits
fromkeys()
method creates a new dictionary from a list. The setdefault()
method returns a value, if a key is present. Otherwise it inserts a key with a specified default value and returns the value. basket = ('oranges', 'pears', 'apples', 'bananas')We have a list of strings. From this list a new dictionary will be constructed.
fruits = {}.fromkeys(basket, 0)The
fromkeys()
method creates a new dictionary, where the list items will be the keys. Each key will be initiated to 0. Note that the fromkeys()
method is a class method and needs the class name, which is {} in our case, to be called. fruits['oranges'] = 12Here we add some values to the fruits dictionary.
fruits['pears'] = 8
fruits['apples'] = 4
print fruits.setdefault('oranges', 11)The first line prints 12 to the terminal. The 'oranges' key exists in the dictionary. In such a case, the method returns the its value. In the second case, the key does not exist yet. A new pair 'kiwis': 11 is inserted to the dictionary. And value 11 is printed to the console.
print fruits.setdefault('kiwis', 11)
$ ./fruits.pyWe receive this output, when we launch the fruits.py script.
{'bananas': 0, 'pears': 0, 'oranges': 0, 'apples': 0}
12
11
{'kiwis': 11, 'bananas': 0, 'pears': 8, 'oranges': 12, 'apples': 4}
The next code example will show, how to add two Python dictionaries.
#!/usr/bin/pythonWe have two dictionaries. They are joined with the
domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary"}
domains2 = { "us": "United States", "no": "Norway" }
domains.update(domains2)
print domains
update()
method. domains.update(domains2)The domains2 dictionary is added to the domains dictionary with the
update()
method. $ ./domains.pyThe result shows all values from both dictionaries.
{'sk': 'Slovakia', 'de': 'Germany', 'no': 'Norway',
'us': 'United States', 'hu': 'Hungary'}
Now we will show, how to remove a pair from a dictionary.
#!/usr/bin/pythonThe items dictionary has 6 key-value pairs. We will delete pairs from this dictionary.
items = { "coins": 7, "pens": 3, "cups": 2,
"bags": 1, "bottles": 4, "books": 5 }
print items
items.pop("coins")
print items
del items["bottles"]
print items
items.clear()
print items
items.pop("coins")The
pop()
method removes a pair with a specified key. del items["bottles"]The
del
keyword deletes a "bottles": 4 pair from the items dictionary. items.clear()The
clear()
method clears all items from the dictionary. $ ./removing.pyExample output.
{'bags': 1, 'pens': 3, 'coins': 7, 'books': 5, 'bottles': 4, 'cups': 2}
{'bags': 1, 'pens': 3, 'books': 5, 'bottles': 4, 'cups': 2}
{'bags': 1, 'pens': 3, 'books': 5, 'cups': 2}
{}
Keys and values
A Python dictionary consists of key-value pairs. Thekeys()
method returns a list of keys from a dictionary. The values()
method creates a list of values. And the items()
method returns a list of key-value tuples. #!/usr/bin/pythonWe demonstrate the above mentioned methods. We also check if a key is present with the
domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary",
"us": "United States", "no": "Norway" }
print domains.keys()
print domains.values()
print domains.items()
print "de" in domains
print "cz" in domains
in
keyword. print domains.keys()We print the list of keys of a domains dictionary with the
keys()
method. print domains.values()We print the list of values of a domains dictionary with the
values()
method. print domains.items()And finally, we print the list of key-value tuples of a domains dictionary using the
items()
method. print "de" in domainsWith the
print "cz" in domains
in
keyword, we check if the "de", "cz" keys are present in the domains dictionary. The return value is either True or False. $ ./keys_values.pyOutput of the example.
['sk', 'de', 'no', 'us', 'hu']
['Slovakia', 'Germany', 'Norway', 'United States', 'Hungary']
[('sk', 'Slovakia'), ('de', 'Germany'), ('no', 'Norway'),
('us', 'United States'), ('hu', 'Hungary')]
True
False
Looping
Looping through the dictionary is a common programming job. This can be done with the for keyword.#!/usr/bin/pythonIn the example, we traverse the domains dictionary to print the keys, values and both keys and values of the dictionary.
domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary",
"us": "United States", "no": "Norway" }
for key in domains:
print key
for k in domains:
print domains[k]
for k, v in domains.items():
print ": ".join((k, v))
for key in domains:This loop prints all the keys of the dictionary.
print key
for k in domains:The second loop prints all values of the dictionary.
print domains[k]
for k, v in domains.items():In the final loop, all keys and values are printed.
print ": ".join((k, v))
$ ./looping.pyOutput of the example.
sk
de
no
us
hu
Slovakia
Germany
Norway
United States
Hungary
sk: Slovakia
de: Germany
no: Norway
us: United States
hu: Hungary
Sorting
Python dictionaries are orderless. This also implies that they cannot be sorted like a Python list. Programmers can create sorted representations of Python dictionaries. In this section, we will show several ways to create a sorted output.Programmers might want to sort the data in a normal or reverse order. They could sort the data by keys or by values.
#!/usr/bin/pythonThe first example provides the simplest solution to have the data sorted by the keys.
items = { "coins": 7, "pens": 3, "cups": 2,
"bags": 1, "bottles": 4, "books": 5 }
kitems = items.keys()
kitems.sort()
for k in kitems:
print ": ".join((k, str(items[k])))
kitems = items.keys()A list of keys is obtained from the dictionary. The list is sorted with the
kitems.sort()
sort()
method. for k in kitems:In the loop we print the sorted keys together with their values from the dictionary.
print ": ".join((k, str(items[k])))
$ ./simplesort.pyThe items dictionary is sorted by its keys.
bags: 1
books: 5
bottles: 4
coins: 7
cups: 2
pens: 3
More efficient sorting can be done with the built-in
sorted()
function. #!/usr/bin/pythonIn the example we print sorted data by their keys in ascending and descending order using the
items = { "coins": 7, "pens": 3, "cups": 2,
"bags": 1, "bottles": 4, "books": 5 }
for key in sorted(items.iterkeys()):
print "%s: %s" % (key, items[key])
print "####### #######"
for key in sorted(items.iterkeys(), reverse=True):
print "%s: %s" % (key, items[key])
sorted()
function. for key in sorted(items.iterkeys()):In this for loop, we print the pairs sorted in ascending order. The
print "%s: %s" % (key, items[key])
iteritems()
function returns an iterator over the dictionary’s (key, value) pairs. for key in sorted(items.iterkeys(), reverse=True):In the second for loop, the data is sorted in descending order. The order type is controlled by the
print "%s: %s" % (key, items[key])
reverse
parameter. $ ./sorting.pyOutput of the sorting.py script.
bags: 1
books: 5
bottles: 4
coins: 7
cups: 2
pens: 3
####### #######
pens: 3
cups: 2
coins: 7
bottles: 4
books: 5
bags: 1
In the next example, we are going to sort the items by their values.
#!/usr/bin/pythonThe example prints the data in ascending and descending order by their values.
items = { "coins": 7, "pens": 3, "cups": 2,
"bags": 1, "bottles": 4, "books": 5 }
for key, value in sorted(items.iteritems(),
key=lambda (k,v): (v,k)):
print "%s: %s" % (key, value)
print "####### #######"
for key, value in sorted(items.iteritems(),
key=lambda (k,v): (v,k), reverse=True):
print "%s: %s" % (key, value)
for key, value in sorted(items.iteritems(),Dictionary pairs are sorted by their values and printed to the console. The key parameter takes a function, which indicates, how the data is going to be sorted.
key=lambda (k,v): (v,k)):
$ ./sorting2.pyFrom the output we can see, that this time the pairs were sorted according to their values.
bags: 1
cups: 2
pens: 3
bottles: 4
books: 5
coins: 7
####### #######
coins: 7
books: 5
bottles: 4
pens: 3
cups: 2
bags: 1
Views
Python 2.7 introduced dictionary view objects. Views provide a dynamic view on the items of a dictionary. They bear similarity to SQL views. When the dictionary changes, the view reflects these changes. Thedict.viewkeys()
, dict.viewvalues()
and dict.viewitems()
methods return view objects. A view is a virtual read-only container. A view does not make a copy of a dictionary.
#!/usr/bin/pythonThree view objects of the dictionary's items, dictionary's keys and dictionary's values are created. We traverse the view with the for loops.
fruits = { 'oranges': 12, 'pears': 5, 'apples': 4, 'bananas': 4 }
vi = fruits.viewitems()
vv = fruits.viewvalues()
vk = fruits.viewkeys()
for k, v in vi:
print k, v
for v in vv:
print v
for k in vk:
print k
vi = fruits.viewitems()The
viewitems()
creates a view of the dictionary's items. for k, v in vi:We traverse the created view and print the keys and values in the for loop.
print k, v
$ ./views.pyOutput of the views.py script.
bananas 4
pears 5
oranges 12
apples 4
4
5
12
4
bananas
pears
oranges
apples
In the next example we show that a view reflects dictionary changes.
#!/usr/bin/pythonA view is created on the fruits dictionary. Two items are deleted from the dictionary. Then we traverse the view to see, if the changes are reflected.
fruits = { 'oranges': 12, 'pears': 5, 'apples': 4, 'bananas': 4}
vi = fruits.viewitems()
for k, v in vi:
print k, v
fruits.pop('apples')
fruits.pop('oranges')
print "########### ##########"
for k, v in vi:
print k, v
vi = fruits.viewitems()A view is created on the fruits dictionary.
fruits.pop('apples')Two items are deleted with the
fruits.pop('oranges')
pop()
method. for k, v in vi:We loop through the view of the fruits.
print k, v
$ ./views2.pyFrom the output we can see, that the changes were reflected in the view.
bananas 4
pears 5
oranges 12
apples 4
########### ##########
bananas 4
pears 5
In this part of the Python tutorial, we have written about Python dictionaries.
0 comments:
Post a Comment