In this Python Tutorial we want to learn about Python Dictionary, python dictionary is collection of data values which is unordered and it is changeable. or we can say dictionary in Python is a collection of key value pairs. Each key is connected to a value, and you can use 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 value in a dictionary. In Python dictionaries are written with curly brackets and they have keys and values.
These are some key points to keep in mind about dictionaries in Python:
- To create dictionary you can use curly braces to enclose the key value pairs separated by commas or use the built in dict() function.
- Each key value pair is separated by colon.
- Keys in dictionary must be unique and immutable (i.e., they cannot be changed after they are created). values can be of any data type.
- You can access individual values in the dictionary using their key.
- You can add new key value pair to the dictionary using the key as the index.
- You can remove key value pair from the dictionary using the del keyword.
- You can find the number of key value pairs in the dictionary using the len() function.
- You can also get list of all the keys or all the values in the dictionary using the keys() and values() methods, respectively.
Dictionaries are very useful for storing and accessing data in flexible and efficient way. They allow you to easily look up values using a unique key, which can make certain types of programming tasks much easier. By understanding how dictionaries work and how to use them effectively, you can take advantage of their strengths and improve the quality and efficiency of your Python code.
Let’s create our first dictionary. in this example we have keys for the dictionary and also values to every keys.
1 2 3 4 5 6 7 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } |
This is the result.
Now if you want to access the value of a dictionary, you can use the key of that dictionary.
1 2 3 4 5 6 7 8 9 10 11 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } print(person['name']) print(person['lastname']) print(person['email']) |
Run the code and this is the result.
As with most new programming concepts, using dictionaries takes practice. Once you’ve worked with dictionaries for a bit you’ll soon see how effectively they can model real world situations.
Adding New Key Value Pair
Now let’s learn how to add new key value pairs in python dictionary. Dictionaries are dynamic structures, and you can add new key value pairs to a dictionary at any time. For example, to add a new key value pair, you would give the name of the dictionary followed by the new key in square brackets along with the new value.
1 2 3 4 5 6 7 8 9 10 |
person = { 'name': 'Parwiz', 'lastname': 'Forogh', 'email':'par@gmail.com' } person['phone']='045454545' print(person) |
Creating Empty Dictionary
Some times you need to create empty dictionary, and then add some items to the dictionary, to start filling an empty dictionary, define a dictionary with an empty set of braces and then add each key value pair on its own line.
1 2 3 4 5 6 7 |
worker = {} print(worker) worker['name'] = "john doe" worker['email']='par@gmail.com' print(worker) print(worker['email']) |
Run the code and this is the result.
Updating the value of Dictionary
To modify a value in a dictionary, give the name of the dictionary with the key in square brackets and then the new value you want associated with that key.
1 2 3 4 5 6 7 |
worker = {} print(worker) worker['name'] = "john doe" worker['email']='par@gmail.com' worker['email'] = 'updated@gmail.com' print(worker['email']) |
This is the result.
Removing Key Value Pairs
When you no longer need the value that’s stored in a dictionary, you can use the del keyword to remove a key value pair.
1 |
del worker['email'] |
Using get() to Access Values
Using keys in square brackets to retrieve the value from a dictionary might cause some problems, if the key doesn’t exist, you’ll get an error, so we can use get to handle this error.
1 |
print(worker.get('email', 'No email key exists')) |