Press "Enter" to skip to content

Python Dictionary

Zigya Acadmey 0

The Python programming language natively implements a number of data structures. Lists, tuples, sets, dictionaries are but some of them. We will be looking at the dictionary data type in this article.

What are dictionaries ?

A dictionary in python is a mapping object that maps keys to values, where the keys are unique within a collection and the values can hold any arbitrary value. In addition to being unique, keys are also required to be hashable.

An object is said to be hashable if it has a hash value (implemented by a __hash__() method) that does not change during the object’s lifetime. Most commonly, we use immutable data types such as strings, integers, and tuples (only if they contain similarly immutable types) as dictionary keys

A dictionary’s data is always enclosed by a pair of curly braces { }, and would normally look like this:

my_dict = {"f_name": "John", "l_name":"Snow", "age":16, "sex":"Male"}

We have created a dictionary named my_dict where each key-value pair is separated by a full colon, with the key-value pairs as:

  • f_name – John
  • l_name – Snow
  • age – 16
  • sex – Male

Dictionary Operations


Accessing the entire Python dictionary

To get the entire dictionary at once, type its name in the shell.

>>> my_dict

Accessing a value

To access an item from a list or a tuple, we use its index in square brackets. This is the python syntax to be followed. However, a Python dictionary is unordered. So to get a value from it, you need to put its key in square brackets.

To get the f_name from the above dictionary, we write the following code.

>>> my_dict["f_name"]
'John'

et()

The Python dictionary get() function takes a key as an argument and returns the corresponding value.

>>> my_dict.get(age)
16

Updating the Value of an Existing Key

If the key already exists in the Python dictionary, you can reassign its value using square brackets.

Let’s take a new Python dictionary for this.

>>> my_dict['age']=10
>>> my_dicy.get('age')
10

Adding a new key

However, if the key doesn’t already exist in the dictionary, then it adds a new one.

>>> my_dict['addr']='NY'
>>> my_dict
{'f_name': 'John', 'l_name': 'Snow', 'age': 10, 'sex': 'Male', 'addr': 'NY'}

Deleting an entire Python dictionary

To delete the whole Python dict, simply use its name after the keyword ‘del’.

>>> del my_dict

Deleting a single key-value pair

To delete just one key-value pair, use the keyword ‘del’ with the key of the pair to delete.

Now let’s first reassign my_dict for this example.

>>> del my_dict['addr']
{'f_name': 'John', 'l_name': 'Snow', 'age': 10, 'sex': 'Male'}

In-Built Functions on a Python Dictionary

A function is a procedure that can be applied on a construct to get a value. Furthermore, it doesn’t modify the construct. Python gives us a few functions that we can apply on a Python dictionary. Take a look.

len()

The len() function returns the length of the dictionary in Python. Every key-value pair adds 1 to the length.

>>> len(my_dict)
4

 sorted()

Like it is with lists and tuples, the sorted() function returns a sorted sequence of the keys in the dictionary. The sorting is in ascending order, and doesn’t modify the original Python dictionary.

But to see its effect, let’s first modify my_dict.

>>> sorted(my_dict)
['addr', 'f_name', 'gsex', 'l_name']

keys()

The keys() method returns a list of keys in a Python dictionary.

>>> my_dict.keys()
dict_keys(['f_name', 'l_name', 'gsex', 'addr'])

values()

Likewise, the values() method returns a list of values in the dictionary.

>>> my_dict.values()
dict_values(['John', 'Snow', 'Male', 'NY'])

items()

This method returns a list of key-value pairs.

>>> my_dict.items()
dict_items([('f_name', 'John'), ('l_name', 'Snow'), ('gsex', 'Male'), ('addr', 'NY')])

clear()

The clear function’s purpose is obvious. It empties the Python dictionary.

>>> my_dict.cleat()

Conclusion

In this article, we took a deep look at Python Dictionary. We first saw how to create, access, reassign, and delete a dictionary or its elements.

Then we looked at built-in functions and methods for dictionaries. After that, we learned about the operations that we can perform on them.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *