Introduction

You've already gotten acquainted with dictionaries, but just like the other data structures Python supports, there are methods and more powerful ways to use them. There aren't as many methods for working with dictionaries as there are for lists, but that's because dictionaries just don't need them. Plus, many of the ones that do exist, work to break down dictionaries into lists and tuples to make them easier to manage. So, those list methods can be used in conjunction with the dictionary ones to create an efficient machine for handling data.

Dictionary Methods

Items, Keys, and Values

These methods work to break down dictionaries into other data structures to make working with them much more manageable. Doing so also gives access to the methods of those data structures. Through these combinations of methods and loops, you can access and manipulate data with ease.

The first method is items(). It breaks down each entry in the dictionary into a tuple and stores them all in a list.
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
distro_install_list = distro_install_command.items()
print(distro_install_list)
print(distro_install_list[3][1])
It all breaks down exactly the way you would expect it to.

The next method gives you access to the keys in the dictionary in the form of a list.
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
distro_names = distro_install_command.keys()

for distro in distro_names:
	print(distro)
The new distro_names list behaves just like any other and can be looped through just the same.

The last method is the same the one before it, but instead of retrieving the keys, this one get the values. It should be pretty obvious by now that it is called values().
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
distro_commands = distro_install_command.values()

for command in distro_commands:
    print(command)

Dictionary Values in Strings

This isn't a method or anything too complex, but situations will arise when you want to insert values directly from a hash into a string, and it doesn't work quite like you'd think. The % operator actually looks at the dictionary as a whole and pulls values from keys within it to substitute them into a string.
book_info = { 'title': 'Learning Python',
			'pages': 342,
			'pub_date': 'November 2016',
			'chapters': 14,
}
print("The book is called %(title)s, and it was released on %(pub_date)s.  It is %(pages)d pages long with %(chapters)d chapters." % book_info)
As you can see, it's just a more convenient way of handling inserting the values from a dictionary. Using the regular method can get very repetitive and cumbersome, even with only a few entries.

Iterating Over Dictionaries

Iterating over dictionaries isn't as simple as you'd probably think. Because dictionaries operate using pairs of values, the numerically based way of looping through lists doesn't quite work. Give it a shot, and see what happens.
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
for distro in distro_install_command:
	print(distro)
So, what happened? It sort of worked, but the only things outputted were the keys. You can probably guess one way to get the values too, and it will work, but it's not all that elegant of a solution.
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
for distro in distro_install_command:
    print("The distro is %s, and it uses the command %s." % (distro, distro_install_command[distro]))
There is a better way to handle this, and it makes use of the items() method. If you remember from before, the items() method creates a list of tuples containing the keys and values from the dictionary.
distro_install_command = {'Debian': 'apt-get',
                        'Ubuntu': 'apt-get',
                        'Fedora': 'dnf',
                        'CentOS': 'yum',
                        'OpenSUSE': 'zypper',
                        'Arch': 'pacman',
                        'Gentoo': 'emerge'
}
for distro, command in distro_install_command.items():
    print("The distro is %s, and it uses the command %s." % (distro, command))
That seems to fly in the face of something you learned previously, doesn't it? How is that for loop creating two temporary variables to access the elements in a two dimensional list? Well, this is actually functionality built into Python for just this sort of thing. Unfortunately, it doesn't always work well with multidimensional lists because it requires that every list within the multidimensional list be the same size. With a dictionary, you know that you are always going to be working with pairs of values, but multidimensional lists could contain lists of any size. That's why this is really only a great solution when working with dictionaries.

Conclusion

By using methods and iteration, you can break dictionaries into a form that is much easier to work with. By combining the functionality of dictionaries with the functionality available to lists and tuples, you gain incredibly powerful tools for handling data.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions