Python Data Structures II - Lists, Dictionaries, Tuples and Sets

In the last article I explained you about Lists in python. In this article I’m going to explain you about Dictionary data structure in python. This article consists of several parts as below.
- What is a dictionary?
- Create a dictionary
- Access elements in dictionary
- Dictionary methods
What is a Dictionary?
Dictionary is a data structure which uses keys and associated values for each keys. In other words, a dictionary is a collection of mapping of objects to a key . Unlike lists, dictionaries don’t retain order. Some of the characteristics of dictionaries are,
- No duplicate keys are allowed
- Keys are case sensitive
- The values in the dictionary can be any type, but keys must be immutable.
Create a dictionary
First lets create a new dictionary.
my_dictionary = {}
Then lets add key/value to the dictionary created.
my_dictionary["key1"] = "one"
my_dictionary["key2"] = 23
my_dictionary["key3"] = ["one","two"]
After adding elements to the dictionary, lets see the dictionary we have created by using the name of the dictionary in the python terminal you use.
my_dictionary
{"key1": "one", "key2": 23, "key3": ["one","two"]}
We can see that my_dictionary has 3 keys as key1, key2 and key3. key1 has a string value, key2 has an integer value and key3 has a list as the value.
Access elements in dictionary
You can access the values in the dictionary by using the relevant key name. Suppose you want to access the value corresponding the key named “key1” which is “one”. Here I’m using the same dictionary created above.
my_dictionary["key1"]
Suppose you want to access the second element(“two”) in the list returned by “key3”. (Keep in mind that second element’s index value is 1)
my_dictionary["key3"][1]
Lets try to access values in the following nested dictionary.
d = {"k1": ["one", 2, {"k2": ["three","four"]}]}
Dictionary “d” has a key “k1” with a list value. List has 3 elements. First element is a string value of “one”. Second is an integer value 2. And the third is a dictionary with a key named “k2” and it has a list as it’s value. Suppose you want to access the first element in that list, this is how you will do it.
d["k1"][2]["k2"][0]
Dictionary methods
Lets define a simple dictionary and try few methods on it.
d = {"key1": 1, "key2": 2, "key3": 3, "key4": 4}
If we want to return a list of all keys in the dictionary we can use keys() on the dictionary.
d.keys()
dict_keys(['key1', 'key2', 'key3', 'key4'])
If we want to return a list of all values in the dictionary we can use values() on the dictionary.
d.values()
dict_values([1, 2, 3, 4])
If we want to return key/values in the dictionary as tuples we can use items() on the dictionary.
d.items()
dict_items([('key1', 1), ('key2', 2), ('key3', 3), ('key4', 4)])
So in this article I have discussed what is a dictionary, creating a dictionary, accessing elements in a dictionary and methods on dictionaries. I hope that this article helps you to understand about the dictionary data structure in python. I will explain other data structures in upcoming articles. Please find the next article on tuples and sets from here related to this article series.