Deep Dive in Machine Learning with Python

Part — V: Dictionaries and Tuples in Python

Rajesh Sharma
Analytics Vidhya
6 min readOct 29, 2019

--

Welcome to the fifth blog of Deep Dive in Machine Learning with Python, in the last blog (Deep Dive in ML with Python — Part-IV) I demonstrated how to work with python lists effortlessly and operations associated with it.

In today’s blog, we will focus on two very prominent python objects(Tuple & Dictionary) and try to learn how to use them efficiently.

Tuples

  • A tuple is a sequence of immutable Python objects
  • Tuples are similar to lists
  • The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square bracket

Problem-1: How to define a tuple?

Solution-1

In the above example, we created two tuples students and student_marks by providing the sequence of values enclosed in parentheses.

Problem-2: How to create an empty tuple?

Solution-2

In this example, we created an empty tuple(e.g. tup1) by just writing the parenthesis(i.e. ()).

Problem-3: How to verify the type of above-created tuple?

Solution-3

In the above example, by using type function you can find out the class of students.

Problem-4: How to perform Slicing and Indexing in a tuple?

Solution-4

Here, similar to lists we can access the elements of a tuple by providing the index position within square brackets.

Problem-5: How to update a tuple?

Tuples are immutable which means we cannot update or change the values of tuple elements. However, we can take a portion of an existing tuple to create a new tuple.

Solution-5

In the above example, first, we created a tuple named accessories. Then, we created a new tuple named sub_acc which holds the elements of accessories from the 4th position with two new elements(i.e. ‘socks’ and ‘suspenders’).

Problem-6: How to obtain the count of an element from a tuple?

Solution-6

In the first cell of the above example, a new copy of accessories has been created which comprises the existing elements of accessories and 5 new elements(i.e. ‘caps’, ‘pocket squares’, ‘hats’, ‘belts’). Then, in the third cell by using the count() method we find out the total number of occurrences of belts.

Problem-7: How to delete an element from a tuple?

Let’s say we want to delete the 3rd and 4th index elements from accessories.

Solution-7

In the above example, in the first cell, we tried to delete an element from accessories, but we got an error that ‘tuple’ object doesn’t support item deletion. It is because tuple is an immutable python object.

Hence, we can follow the approach used in the second cell to eliminate the 4th element of the tuple.

Problem-8: How to retrieve the index of any tuple element?

Solution-8

In the above example, we obtained the index value of element ‘caps’ from new_accessories. Similarly, the index value of the element ‘hats’ retrieved from accessories.

Problem-9: How to delete a tuple?

Solution-9

In the above example, by using the del function we deleted the tuple students.

Tuple Operations

Through the below examples, you will be able to understand some of the operations associated with Tuple.

Example-1
Example-2
Example- 3 and 4

Dictionary

  • It is a python object which stores the data in key and value pairs
  • Each key is separated from its value by a colon (:), the items are separated by commas, and they all are enclosed in curly braces

Keys of a dictionary are unique whereas values may not be. The values of a dictionary can be of any type, however, the keys must be of an immutable data type such as strings, numbers, or tuples

Problem-1: How to define a dictionary?

Example-1

In this example, we created a python dictionary score_card which contains two keys ‘student_name’, ‘marks’ and their values as lists.

Problem-2: How to access the keys of a dictionary?

Example-2

Here, in the above example, by using the keys() method we can obtain the keys of the dictionary.

Problem-3: How to access the items of a dictionary?

Example-3

In this example, we used the values() method to retrieve the values from the score_card dictionary.

Problem-4: How to retrieve all the keys and values of a dictionary within a list?

Example-4

Here, in the above example, by using items() method we listed all the keys and values of the score_card dictionary in a list format.

Problem-5: How to update the values against a key in a dictionary?

Example-5

In this example, we use the update() method to update the values for the key ‘marks’ of the score_card dictionary.

Problem-6: How to access the values of a key from a dictionary?

We can access the values of a key in below manner:

Solution-6.1

In this example, similar to lists, we accessed the dictionary key by writing it within square brackets and it displayed the value associated with it.

Solution-6.2

In this example, we used the get() method to obtain the values associated with a key.

If you passed a key to get() method that does not exist in a dictionary then it will return None. Refer to the third cell of the above example.

Problem-7: How to find the length of a dictionary?

Solution-7

In the above example, by using len() function we found out the length of the dictionary score_card.

The length of a dictionary is equivalent to the total number of every key-value pair.

Problem-8: Can we have multiple entries of a key in the dictionary?

Solution-8

As shown in the above example, if we provide key multiple times in a dictionary then the key will hold only the last provided value.

Problem-9: How to clear the dictionary?

Solution-9

In the above example, we remove all the keys and values of a dictionary new_dict by using the clear() method.

Problem-10: How to delete the dictionary?

Solution-10

In the above example, by using the del function we deleted the dictionary new_dict.

Congratulations, we come to the end of this blog, to summarize, we covered how to efficiently work with python tuple and dictionary.

If you want to download the Jupyter Notebook of this blog, then kindly access below GitHub repository:

https://github.com/Rajesh-ML-Engg/Deep_Dive_in_ML_Python

Thank you and happy learning!!!!

Blog-6: List Comprehensions and Functions in Python

--

--

Rajesh Sharma
Analytics Vidhya

It can be messy, it can be unstructured but it always speaks, we only need to understand its language!!