DEV Community

Cover image for Python dictionary and dictionary methods
Baransel
Baransel

Posted on

Python dictionary and dictionary methods

Sign up to my newsletter!.

What is Dictionary?

We have said that there are mutable (modifiable) data types that contain different data types such as Tuple and List data types in dictionaries and are shown with curly braces.

Dictionaries are a little different because Dictionaries consist of two parts; keys and values, value part can contain all data type but keys part can only be of string and int type.

How to Use Dictionaries?

Before I show its usage, Lets explain why dictionary; Imagine that you are making an English dictionary, how would it be without a database. You have to write an if condition for each word. Each word is a conditional cycle, which makes the program tiring. Let me show you with an example;

"""Romanian Dictionary Application"""
word = input("Enter the english word: ")

if word == "Computer":
    print("Calculator")
elif word == "School":
    print("Şcoală")
elif word == "Memory":
    print("Memorie")
elif word == "House":
    print("Casa")
elif word == "Bus":
    print("Autobuz")
elif word == "Car":
    print("Mașină")
Enter fullscreen mode Exit fullscreen mode

You will probably have written code like this.

Let's write the same application with Dictionaries.

"""Romanian Dictionary Application"""
word = input("Enter the english word: ")

dictionary = {"Computer":"Calculator","School":"Şcoală","Memory":"Memorie",
"House":"Casa","Bus":"Autobuz","Car":"Mașină"}
Enter fullscreen mode Exit fullscreen mode

In this way, you will have written more neat and readable code, and since Dictionaries are mutable (modifiable) data types, they can be easily added, deleted, changed, etc. we can do the work.

Accessing Dictionary Items

Dictionaries are based on key=value relationships, and every value has a key value. Then we can reach the value with the key we know. Let's give an example right away;

dictionary = {"Computer":"Calculator","School":"Şcoală","Memory":"Memorie",
"House":"Casa","Bus":"Autobuz","Car":"Mașină"}

print(dictionary["Computer"])

# Calculator
Enter fullscreen mode Exit fullscreen mode

Dictionary Methods

Let's list the dictionary methods immediately;

['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__',
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
 '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
 '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
 '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys',
 'pop', 'popitem', 'setdefault', 'update', 'values']
Enter fullscreen mode Exit fullscreen mode

If we omit __X__ private methods

['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Enter fullscreen mode Exit fullscreen mode

We will cover their methods.

keys Method

Continue this post on my blog! Python dictionary and dictionary methods.

Sign up to my newsletter!.

Top comments (0)