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
We need your assistance to stay live
This week Top Posts
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX : Wow! Early last week it was all over the internet, if you haven't heard it yet then this is the t...0 comments |
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...1 comment |
- 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 "Fix E: Could not open file /var/lib/dpkg/status" : QUESTION: I get the following error when I use sudo apt-get update: E: Could not open file /var/...0 comments |
- Install Sublime Text 3 In Fedora Easily With Fedy : Sublime Text is a sophisticated text editor for code, markup and prose. In this tutorial we are goi...0 comments |
- Install nSnake Game In Terminal : From github repo nsnake is a clone of the classic snake game that we all used to play on our cellp...0 comments |
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
- How To “Fix E: Could not open file /var/lib/dpkg/status”
- Wow! Linux Foundation To Offer Free Linux Course This Summer Worth $2,400 on EdX
- Fedora 21 Scheduled For Release In October
- Install Uget Download Manager In Ubuntu, Fedora, Debian
- Download Elementary OS ‘Luna’ Official Wallpapers
- Install Power Commands 0.1.5 On Ubuntu 14.04/13.10/12.10/12.04
- 5 Android Apps that’s Going to Make Running Your Business Less Stressful
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .