DEV Community

Kayode
Kayode

Posted on • Edited on

Python Dictionary

A dictionary is a key-pair value. It resembles a list but rather than having an integer as a memory address, a dictionary makes use of a key to store access to a value.

For instance, you can have a dictionary using English words as the key and using the Spanish interpretation as the value.
The define a dictionary you can define it using curly braces{} or the dict() function.

Checking the existence in a dictionary

To get the existence of a key in a dictionary, you can make use of the in operator.

eng2sp = dict()
eng2sp['one'] = 'uno'

'two' in eng2sp # False

'one' in eng2sp # True

Enter fullscreen mode Exit fullscreen mode

To check the existence in a value. You would covert get he values from the dict using the .values() method and convert the type to a list. for instance

vals = list(eng2sp.values())
Enter fullscreen mode Exit fullscreen mode

Getting the number of char occurrence in a word with dictionary

word = 'pineapple'

word_dict = dict()

for char in word:
    if char not in word_dict:
        word_dict[char] = 1
    else:
        word_dict[char] += 1
Enter fullscreen mode Exit fullscreen mode

dictionary provides a method called .get() that returns the value of a key in an object or a default value passed as an argument if not provides. Replacing it in the code above we can have a simple one code without a coditional.

word = 'apple'

word_dict = dict()

for char in word:
    word_dict[char] = word_dict.get(char, 0) + 1
Enter fullscreen mode Exit fullscreen mode

Dictionary and reading the words occurrence in a file

fname = input("Enter the file name: ")

try:
    fhand = open(fname)
except:
    print("An error occurred when openning the file")
    exit()

word_dict = dict()
for line in fhand:
    line = line.rstrip()
    words = line.split(" ")

    for word in words:
        word_dict[word] = word_dict.get(word, 0) += 1
Enter fullscreen mode Exit fullscreen mode

The code above works file but it doesn't recognize punctuations and alphabets cases as the split use the arguments to divide the strings into a list. To solve this issues, we will need the convert the strings to lower case, remove all the puntuations and perform our dictionary operations.

To remove or delete part of the strings, we can make use of the line.translate(.maketrans(fromstr, tostr, deletestr)). The method replaces fromstr with tostr in the string and deletes the deletestr. We would get the list of punctuations from the string module. Rewriting the code:

import string

fname = input("Enter the file name: ")

try:
    fhand = open(fname)
except:
    print("An error occurred when openning the file")
    exit()

word_dict = dict()
for line in fhand:
    line = line.rstrip()
    line = line.translate(maketrans("", "", string.punctuation))
    line = line.lower()
    words = line.split(" ")

    for word in words:
        word_dict[word] = word_dict.get(word, 0) += 1
Enter fullscreen mode Exit fullscreen mode

Reference

Python 4 Everyone

Top comments (0)