Python’s dictionary data type provides a flexible way to access and organize data. It allows us to connect pieces of related information. Using dictionary we can model a variety of real-world objects more accurately.
A dictionary in Python is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key’s value can be a number, a string, a list, or even another dictionary.
In fact, you can use any object that you can create in Python as a value in a dictionary. In Python, a dictionary is wrapped in braces, {}, with a series of key-value pairs inside the braces.
Like a list, a dictionary is a collection of many values. But unlike indexes for lists, indexes for dictionaries can use many different data types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair.
It's time to create a dictionary, say of your programmer friends.
programmers = {'Bob':'Java', "Ted":'C', "Joey":'C++', "Chris":'Python',"Mike":'R'}
As shown in the programmers dictionary we just created every key is connected to its value by a colon, and individual key-value pairs are separated by commas. As we know key-value pair is a set of values associated with each other. When you provide a key, Python returns the value associated with that key. Thus if we write -
print (programmers['Bob'])
Pyhton will return the value associated with 'Bob' which is Java. See the screenshot below for the program and output-
The simplest dictionary has exactly one key-value pair, as shown in the code below-
user1 = {'ename':'Nagaraju'}
This dictionary stores one piece of information about COVRI_EMPLOYEE, namely the user1’s name. The string 'ename' is a key in this dictionary, and its associated value is 'Nagaraju'. Thus if I want to accessing the value from this dictionary I'll simply code -
print(user1['ename'])
which returns the value associated with 'ename' i.e is 'Nagaraju'. We can have an unlimited number of key-value pairs in a dictionary, thus we can add more key-value pairs to our dictionary on need basis.
How to add new Key-Value Pairs
At this stage our user1 dictionary only contains one key-value pair for name but now I realize I also need to store user's age in my dictionary. To do so give the name of the dictionary followed by the new key in square brackets along with the new value.
user1['age'] = 40
See the screenshot below for the program and output-
To see the modified user1 dictionary the syntax is -
print(user1)
Which will display the following output -
In fact we can start with an empty dictionary and add new items as per requirement. For example-
enemy1 ={}
This dictionary will be used to store details about an enemy for a game. Now I can add some key-value pairs to make my dictionary meaningful. I am adding -
enemy1['color'] = 'red'
enemy1['points'] = '10'
The idea is if a player shoots enemy1 whose color is red he'll score 10 points. See the screenshot below for the program and output-
Often we use empty dictionaries when storing user-supplied data in a dictionary or when we write code that generates a large number of key-value pairs automatically.
How to modify values in a dictionary
In the enemy1 dictionary create above, I feel that the value associated with the color key must be changed from red to blue. This is how we can modify value in a dictionary-
enemy1['color'] = 'blue'
See the screenshot below for the program and output-
Similarly we can modify the points from 10 to 20 . Try to add some more Key-value pairs and then modify some of them.
How to remove Key-value pairs
We made a dictionary of programmers in the beginning of this post. Now one of the programmer 'Ted' has quit thus I need to remove him from my dictionary. The syntax to do this is shown below-
del programmers["Ted"]
The del needs the name of the dictionary and the key that you want to remove.
See the screenshot below for the program and output-
Thus when we no longer need a piece of information that’s stored in a dictionary, we can use the del statement to permanently remove a key-value pair.
So far we have covered the basics about Python Dictionary data type. Do some more practice with Dictionary and then we'll cover some advance topics related to dictionary.
Keep studying! Python is easy to learn.
A dictionary in Python is a collection of key-value pairs. Each key is connected to a value, and you can use a key to access the value associated with that key. A key’s value can be a number, a string, a list, or even another dictionary.
In fact, you can use any object that you can create in Python as a value in a dictionary. In Python, a dictionary is wrapped in braces, {}, with a series of key-value pairs inside the braces.
Like a list, a dictionary is a collection of many values. But unlike indexes for lists, indexes for dictionaries can use many different data types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair.
It's time to create a dictionary, say of your programmer friends.
programmers = {'Bob':'Java', "Ted":'C', "Joey":'C++', "Chris":'Python',"Mike":'R'}
As shown in the programmers dictionary we just created every key is connected to its value by a colon, and individual key-value pairs are separated by commas. As we know key-value pair is a set of values associated with each other. When you provide a key, Python returns the value associated with that key. Thus if we write -
print (programmers['Bob'])
Pyhton will return the value associated with 'Bob' which is Java. See the screenshot below for the program and output-
The simplest dictionary has exactly one key-value pair, as shown in the code below-
user1 = {'ename':'Nagaraju'}
This dictionary stores one piece of information about COVRI_EMPLOYEE, namely the user1’s name. The string 'ename' is a key in this dictionary, and its associated value is 'Nagaraju'. Thus if I want to accessing the value from this dictionary I'll simply code -
print(user1['ename'])
which returns the value associated with 'ename' i.e is 'Nagaraju'. We can have an unlimited number of key-value pairs in a dictionary, thus we can add more key-value pairs to our dictionary on need basis.
How to add new Key-Value Pairs
At this stage our user1 dictionary only contains one key-value pair for name but now I realize I also need to store user's age in my dictionary. To do so give the name of the dictionary followed by the new key in square brackets along with the new value.
user1['age'] = 40
See the screenshot below for the program and output-
To see the modified user1 dictionary the syntax is -
print(user1)
Which will display the following output -
In fact we can start with an empty dictionary and add new items as per requirement. For example-
enemy1 ={}
This dictionary will be used to store details about an enemy for a game. Now I can add some key-value pairs to make my dictionary meaningful. I am adding -
enemy1['color'] = 'red'
enemy1['points'] = '10'
The idea is if a player shoots enemy1 whose color is red he'll score 10 points. See the screenshot below for the program and output-
Often we use empty dictionaries when storing user-supplied data in a dictionary or when we write code that generates a large number of key-value pairs automatically.
How to modify values in a dictionary
In the enemy1 dictionary create above, I feel that the value associated with the color key must be changed from red to blue. This is how we can modify value in a dictionary-
enemy1['color'] = 'blue'
See the screenshot below for the program and output-
Similarly we can modify the points from 10 to 20 . Try to add some more Key-value pairs and then modify some of them.
How to remove Key-value pairs
We made a dictionary of programmers in the beginning of this post. Now one of the programmer 'Ted' has quit thus I need to remove him from my dictionary. The syntax to do this is shown below-
del programmers["Ted"]
The del needs the name of the dictionary and the key that you want to remove.
See the screenshot below for the program and output-
Thus when we no longer need a piece of information that’s stored in a dictionary, we can use the del statement to permanently remove a key-value pair.
So far we have covered the basics about Python Dictionary data type. Do some more practice with Dictionary and then we'll cover some advance topics related to dictionary.
Keep studying! Python is easy to learn.
0 comments:
Post a Comment