Thursday, January 23, 2020

Python Practice problem 3: Determining the overlaps of two lists.

Problem:

This is the practice problem 5 from https://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html. The problem is -



1. We need to make two lists, the items of the lists might be unique or have common items within them.


2. We need to write a program that returns a list containing only the elements that are common between the lists while not considering the duplicates.


For example, we are given two lists- list_A and list_B as the following


list_A=[1,3,5,6,7,8,9,10,13,15,20]


list_B=[2,4,6,8,9,10,12,14]


Our program should be able to find duplicates.


Solving Part:

The problem is pretty straight forward, but our objective is to explore different ways and learn python through problem-solving.

While solving problems, one problem I often fetch is indexing error which is caused by putting variable() instead of variable[]. The problem arises because I came from a Matlab background where the function calling and the variable indexing uses the same "()" which is confusing actually.


Anyway, let's go to coding. The first quick draft is shown at the following which is not taking care of duplicate values. The first thing comes to mind is the use of Brute Force. which is -


1. Take any of the two lists and iterate through it

2. While iterating through the items, check for the existence in the second list using "in" keyword.

The code is shown below-




You can obviously see the problem here. Look at the program output. [6, 8, 9, 10, 10]. We have duplicates in the result. It is expected. The last piece of code I  want to add to this program is, well I know it's not the best way to do this, is using a set operation on the generated list to remove the duplicate items i.e., to keep the unique items only in the list.


So the final version is here.


Learnings:


Few things are important to note from here


"in" operator:

The "in" operator in python is for checking membership of value against a sequence. For example,
If we declare a list and then if we want to check for the existence of an item in that list we can use this keyword "in". I am writing these expressions for clarification

There are two operators which are called membership operators. 

a. in operator
b. not in operator
About 'not in' operator, It basically does the opposite of  "in" operator. i.e., the expression containing it evaluates to true if it does not find the variable in the sequence.

We need to learn about Identity operators too. There are two identity operators like the membership operator.


1. 'is' operator: It checks if the object on the left side of  'is' is pointing to the same object on the right side. Personally, at first, I was confused about this operator.  But, when I think of the variables as a just human-readable version of memory tags,  it becomes a little bit clear. The following example and the output is a good way to understand the behavior of the 'is' operator. Here we took three variables p,q, and r. Then we initialized p and q with the same content. In the third line of code, we make p and r equal. As expected, although the content of the p and q are the same, they are pointing to the different locations in the memory. The code snippets will clarify the rest-



2.' is not' operator: just the opposite of the 'is' operator. 






No comments:

Post a Comment