Dictionaries in Python
A dictionary is a container that stores Python objects and each item of the dictionary consist of a key and value. Python dictionaries are also known as associative arrays or hash tables. Curly brackets distinguish a dictionary from other objects. The following example shows a dictionary with three items.
Items in a dictionary are separated by commas and each key is separated by a colon from its own value. The keys of a dictionary should be unique while values may not be. Keep in mind that values of a dictionary can be of any data type, but the keys must be of an immutable data type. How can one access dictionary values? To access dictionary values you need the key that belong to the specific value and use square brackets like shown in Figure 2.
If you want to find how many items are there in a dictionary you can find that by using the len() Python built-in function. An example of finding dictionary length is shown in Figure 3.
Sometimes, when you program in Python you may want to store dictionary items in a list. The dict_name.items() method comes in handy in this kind of situation. This method returns a list with all the dictionary items where each item is inside a tuple. You can learn how to use this useful method from Figure 4.
There are many other useful methods which can be useful to you while you program in Python programming language. How about deleting a dictionary or some items of a dictionary? You can do that with the del statment, to delete the entire dictionary use del and after that the name of the dictionary. If you want to delete an item from the dictionary you can do that with the del statement and after this statement you have to put the dictionary name and the key inside square brackets, like you want to access the value of this item. Figure 5 shows you how to delete the entire dictionary and delete dictionary items with the del statement.
This first article about dictionaries in Python is enough for beginners in Python programming language. After we have finished with introducing basics of Python programming language we will move into more advanced stuff.
Like us on Facebook
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- How To Show Username On Panel In Ubuntu 13.04/13.10 : By default usernames are not displayed on panel in Ubuntu 13.04 and 13.10, In this simple tutorial w...0 comments |
- cowsay And fortune Combined Together : Hi linux geeks! In this article I will teach you a nice trick you can perform in the terminal with ...0 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Install and Configure Samba share in Ubuntu 13.10 'Saucy Salamander' , 13.04| Howto : Updated 05-04-2013: One of the most asked features for Samba is a graphical user interface to help w...0 comments |
- cowsay And fortune Combined Together
- How To Show Username On Panel In Ubuntu 13.04/13.10
- Setup DNS Server On openSUSE 13.1
- TeamViewer 9 Final Has Been Released!
- Twelve Vulnerabilities Have Been Fixed In Ubuntu 12.04, Time To Update
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .