Access dictionary keys and values in Python
In this articles, we will look to Dictionary variables in Python.
In earlier article, we saw the dictionary type variables in Python. We recall earlier part that dictionaries are a compound type variable similar to C, C++ and C# arrays but can store different type of variables like int, string and others. Python dictionaries contain values like key-pairs. A dictionary key can be any Python type. Mainly numbers or string s. Value can be any arbitrary Python object.
In this part, we will see how to access dictionary keys and values of them.
Dictionary are declared as follows:
dict_planets = {}
dict_planets['one'] = "Mercury"
dict_planets['two'] = "Neptune"
print(dict_planets)
print(dict_planets['one'])
print(dict_planets.keys())
print(dict_planets.values())
Lets see Access Python Dictionary keys and values in an Python example.
In above example, we created a Dictionary variable in Python, and access key and values, print it to terminal.