Python : Exploring a dictionary

Hello Friends, today in this article we are going to discuss about dictionary in Python
Dictionary is known to be a king of data structures in Python. It is the backbone of every Python object and it is the only standard mapping type.
The dictionary represent a group of elements arranged in the form of key value pairs. There are special objects or dictionary views : Keys, values and items.
What is dictionary?
The dictionary is an unordered collection that contains key:value pairs separated by commas inside curly brackets ({}).
Some Features of dictionaries in Python
- Keys are unique. Dictionary keys must be unique.
- Dictionaries are unordered
- Keys and it’s values are separated by colon(:)
What is the use of dictionary
Dictionary in Python is used to store data in the keys. Keys in dictionary are always unique it helps us to identify the values when the values are same.
for example –
Key | value |
1111 | Mack |
1112 | Mack |
1113 | Zen |
In the above table you can see that the keys are unique and the two values are same but the help of keys we can identify the value
Real life example of Keys –
- Telephone number
- Car number
- Roll number
In this examples the value can be same but the keys are always unique.
Creating a dictionary
Creating a dictionary with integer keys
In this program we are creating the dictionary with integer keys. The all the keys will be integer.
integer Keys = {1: 'Knowledge', 2: 'Junction'}
print("\nDictionary with the use of Integer Keys: ")
print(integerKeys)
------------------------------------------------------
In[1]: runfile(...)
Dictionary with the use of Integer Keys:
{1: 'Knowledge', 2: 'Junction'}
Creating a dictionary with mixed keys
In this program we are using the all the types of keys – mixed keys
mixedKeys = {'Name': 'Knowledge', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(mixedKeys)
-------------------------------------------------------
In[1] runfile (...)
Dictionary with the use of Mixed Keys:
{'Name': 'Knowledge', 1: [1, 2, 3, 4]}
Creating a dictionary with dictionary()
knowledgejunction = dictionary({1: 'Knowledge', 2: 'Junction'})
print("\nDictionary with the use of dictionary(): ")
print(knowledgejunction)
Creating a dictionary with each item as pair
pairdictionary = dict([(1, 'Knowledge'), (2, 'Junction')])
print("\nDictionary with each item as a pair: ")
print(pairdictionary)
--------------------------------------------------------
In [1] runfile(...)
Dictionary with each item as a pair:
{1: 'Knowledge', 2: 'Junction'}
Nested dictionary
What is Nested dictionary?
In Python, a nested dictionary is a dictionary inside a dictionary. It’s a collection of dictionaries into one single dictionary.
Creating a Nested Dictionary
nesteddictionary = {1: 'Hello', 2: 'world',
3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'World'}}
print(nesteddictionary)
----------------------------------------------------------
In [1] runfile (...)
{1: 'Hello', 2: 'world', 3: {'A': 'Welcome', 'B': 'To', 'C': 'World'}}
To access the elements using [] syntax
people = {1: {'name': 'Joseph', 'age': '12'},
2: {'name': 'Marie', 'age': '11'}}
print(people[1]['name'])
print(people[1]['age'])
-------------------------------------------------------
In [1] runfile (...)
Joseph
12
We have very good series of Python articles, please have a look – https://knowledge-junction.in/category/python/
Thank you for reading this article
Have a nice day 🙂
Nice article Sayyam