What are Python dictionary functions

We have learned how a Python dictionary works. However, we use Python dictionary functions without knowing that they are built-in methods.

In the previous sections we have seen how the 

Python Set and Dictionary belong to the collections or data structures. We have also discussed lists and tuples.

However, lists and tuples differ in nature. The lists are mutable, whereas the tuples are not.


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.


The Python Set and Dictionary are also different. 

Moreover, we only use a dictionary when the amount of data is huge. 

The data structure is also different in nature.

Let us see a few examples of Python dictionary functions.

lat_and_long = {'lat': '33.8688° S', 'lang': '151.2093° E'}

print("Dictionary before:", lat_and_long)

# removes all the items from the dictionary
lat_and_long.clear()

print("Dictionary after:", lat_and_long)

# output:
Dictionary before: {'lat': '33.8688° S', 'lang': '151.2093° E'}
Dictionary after: {}

In the above code, the clear() method makes a dictioanry empty clearing all items at one go.

For huge data, a python dictionary is the ideal solution with an added advantage of key value pairs.

Using curly braces and keeping each item in a pair “key : value” is the structure. 

Let us see some more examples of dictionary functions.

lat_and_long = {'lat': '33.8688° S', 'lang': '151.2093° E'}
lat_and_long_copy = lat_and_long.copy()
print(lat_and_long_copy)

#output:
{'lat': '33.8688° S', 'lang': '151.2093° E'}

In the above code we have used the copy() method. It’s a shallow copy of the whole dictionary.

In a dictionary key and value can be of any value.

However, we can use the get() method to extract one key.

only_lat = lat_and_long.get('lat')
# The method returns the value for the specified key if the key is in the dictionary.
print(only_lat)

# output:
33.8688° S

Since the key is in dictionary, it returns the value associated with the specified key.

Firstly, the Dictionary is a built-in Python Data Structure that we can change.

As long as we think of data structure in Python, any dictionary may be similar in spirit to List, Set, and Tuples.

The main point is a dictionary is not indexed by a sequence of numbers but indexed based on keys.

Each key should be unique and must point to an associated value. 

We can use the key() functions to return all keys as a list item.

all_keys = lat_and_long.keys()

print(all_keys)

# The method extracts the keys of the dictionary and returns the list of keys.

# output:
dict_keys(['lat', 'lang'])

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

Using items() function we can get the key value tuple pairs as a list item.

print(lat_and_long_copy.items())

# The method displays a list of dictionary's (key, value) tuple pairs.

# output:
dict_items([('lat', '33.8688° S'), ('lang', '151.2093° E')])

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

In the coming section we will learn more about python dictionary functions.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

C, C++, Java and Game Development

Twitter

Comments

Leave a Reply