 Python Data Structures - Dictionary and Sets | Brain Mentors Skip to content

# Python Data Structures – Dictionary and Sets

### Dictionary

A dictionary is a collection which is unordered, mutable. In Python, Dictionary is the collections of key: value pair which are enclosed inside {}(curly brackets).

#### Methods in Dictionary

• items() : Returns a list containing a tuple for each key value pair.
• keys() : Returns a list containing the all the keys existing in the dictionary.
• values() : Returns a list containing the values in the dictionary
• copy() : Returns a copy of the dictionary.
• popitem() : Removes the last key-value pair from the dictionary.
• pop() : Removes the value which is bind to the specific key.
• clear() : Removes All the key:value pair of the dictionary.

#### Dropping the dictionary

This is used to delete the entire dictionary from the memory.

#### Example on Dictionary

Lets suppose there are nth students in the class .
So Dictionary for that will look like:

```StudentDB = {"rollNo":[101,102,103],
"Name":["Ramesh","Suresh","Raju"],
"Marks":{"physics":[90,94,98], "chemistry":[90,91,99], "maths":[85,93,96]}}```
```Now, You have to Find average for each student.
So , create an empty result list in which we will store average marks of each student.
average = []

Now, we will start traversing our dictionary using for loop.
and add marks , divide the added marks by total number of marks for each student.and finally we will get average marks for each student and we will store them in list named average.
for i in range(len(StudentDB["rollNo"])):
avg = (StudentDB["Marks"]["physics"][i]+StudentDB["Marks"]["chemistry"][i]
+StudentDB["Marks"]["maths"][i])//len(StudentDB["Marks"])
average.append(avg)
Now, append the list containing average marks in the dictionary named StudentDB
StudentDB["average"] = average

Now, We have to find the Grade for each Student according to their average marks.
So create an empty list for grades
Result=[]
So iterate over the list which contains average and then use if-else to assign grade to each and every student and append grade for each student in Result List.
for i in range(len(average)):
if(average[i]>=90):
Result.append('A+')
elif(average[i]>=80):
Result.append('A')
elif(average[i]>=70):
Result.append('B+')
elif(average[i]>=60):
Result[i].append("C")
elif(average[i]>=50):
Result[i].append("D")
else:
Result[i].append("F")
Now, Append the Result in the Dictionary StudentDB
StudentDB["Result"]=Result

Now, Output is as Follows:```

### Sets

A set is a collection which is unordered and Indexless. In Python sets are written with curly brackets.

#### Some Methods used in Sets

• Union

Union method is used to find all the values without any kind of repition which lies in two sets .
In simple words, Union means All, So using union between two sets, we will get All but unique values

• Intersection

Intersection method is used to find all the values which are common between two sets .

• DIfference()

syntax: SetA.difference(SetB)
Here, Difference method will remove all values from SetA which also belong to SetB.

• clear()

clear() method removes all elements from the set.

• copy()

Returns the copy of set.

#### Finding Text Similarity using Sets

Here, We have to find the Similarity between two Strings that how much identical are they ?

Text1 =”Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language.”

Text2 =”What is Python? Python is an object-oriented programming language created by Guido Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries and is extensible to C or C++. Many large companies use the Python programming language include NASA, Google, YouTube, BitTorrent, etc.

• 2..Now, Break both strings into tokens so that we can take out all the words from our String
• 3. Remove Stop Words like is,am,are etc from both String Tokens because comparing Stopwords is not a fair deal coz they are common and found everywhere .

Here is the example of Stopwords

So, for that ,import stopwords from nltk

• 4. Now, remove stopwords from token1 and token2 by iterating token1 , token2 with stopwords set
• 4. Now , convert lists words1, words2 into sets
• 5. Now, In Order to find the Similarity, we have to know a formula knows as Jaccards Distance Formula

JACCARDS DISTANCE (Similarity) = len(set1 Intersection set2)/len(set1 union set2)

Now, we have to implement this formula using python code.

Now, The similarity will comes between 0 and 1.
1 means both Strings are 100% identical
0 means both are 0% identical
If we want our answer in percentage, then multiply the similarity by 100