Dictionary functions in Python

Dictionary functions are a few built-in methods in Python. Using these functions we can do many operations on a dictionary.

However, in the previous section we have seen how a python dictionary behaves as a mutable data structure.

But promised to show the delete method in the later section.

Therefore, before discussing the dictionary functions in python, let us see how we can delete any item from a dictionary.

Removing any item from a Python dictionary is quite easy. We use the ‘pop()’ method.

Let us see some examples first.

After that we will discuss it.

# How to remove elements from a dictionary

# Let's create a dictionary of odd items
odd_items = {1: 1, 2: 3, 3: 5, 4: 7, 5: 9}

# Let's remove a particular item, returns its value

print(odd_items.pop(2))

print(odd_items)

# remove an arbitrary item, return (key,value)

print(odd_items.popitem())

print(odd_items)

# remove all items
odd_items.clear()

print(odd_items)

# we can alsp delete the dictionary itself
del odd_items

# If we call a non-existing dictionary, it will throw an error
print(odd_items)

As we can see, we have created a dictionary of odd items first.

Next, we call the pop() method and provide the key. As a result it returns the value first, and then we find that the particular key and value pair is missing. Right?

print(odd_items.pop(2))

print(odd_items)

Here goes the output with missing key and value pair.

3
{1: 1, 3: 5, 4: 7, 5: 9}

First it returns a value.

Next from the dictionary, that particular key value pair has vanished.

The next method, the ‘popitem()’ method, it can remove any arbitrary item from the dictionary.

print(odd_items.popitem())

print(odd_items)

# output:
{1: 1, 3: 5, 4: 7}

It has removed the last pair.

We can also remove all items of any dictionary at once, using the clear() method.

And finally when we use the ‘del’ keyword, it deletes the whole dictionary. 

However, if we call the deleted dictionary, it will throw an error.

The total output looks like this.

3
{1: 1, 3: 5, 4: 7, 5: 9}
(5, 9)
{1: 1, 3: 5, 4: 7}
{}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-970a9dfc9de7> in <module>
     25 
     26 # If we call a non-existing dictionary, it will throw an error
---> 27 print(odd_items)

NameError: name 'odd_items' is not defined

On the other hand, Python comes with many built-in methods that might come to our help.

It will take time to discuss all the built in methods in one go.

We have already used some of them .


If you are a complete beginner your journey to learn TensorFlow might start from here.

For the TensorFlow beginners we have a dedicated category – TensorFlow for Beginners.

But besides that, you may need to learn several other machine learning and data science libraries.

As a result, you may check these categories as well – NumPy, Pandas, Matplotlib.

However, without learning Python, you cannot learn the usages of these libraries. Why? Because they all use Python as the Programming language.

Therefore please learn Python at the very beginning and start learning TensorFlow.

And, finally please check our Mathematics, Discrete Mathematics and Data Structures categories specially. We have tried to discuss from basic to intermediate level so that you can pick up the core ideas of TensorFlow.


Let’s see another example now. This is a very important dictionary function. 

The ‘update([ key: value])’ method can update any dictionary.

Here although we have not overwritten the existing key value pair, we can do that too.

Let’s see an example.

names = {1: 'John', 2: 'Margaret'}
more_names = {3: 'Json', 4: 'Cate'}

names.update(more_names)


print(names)

# output
{1: 'John', 2: 'Margaret', 3: 'Json', 4: 'Cate'}

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Flutter, Dart, Python and TensorFlow

C, C++ Java, and Game Development

Twitter

Comments

Leave a Reply