PYTHON DICTIONARIES

adewaleomosanya

adewaleomosanya

Posted on September 14, 2023

PYTHON DICTIONARIES

PYTHON DICTIONARIES
The dictionaries are like a little in-memory database, Dictionaries are Python’s most powerful data collection. It lets us do fast database-like operations on Python. Dictionaries have different names for different languages

  • PHP - Associative arrays

  • Java - property or hash map

  • Property bag - C#

So the idea of the dictionary is putting more than one thing in it and has ways of indexing it. We index the thing we put in the dictionary with a look-up tag. Dictionaries are enclosed in curly braces {} and consist of one or more key-value pairs separated by colons. For example

my_dict = {
"name": "John",
"age": 30,
"City": "New York"
}
Enter fullscreen mode Exit fullscreen mode

In this dictionary, name, age, and city are keys while John, 30, and New York are their values respectively.
You can also make an empty dictionary using empty curly braces

my_dict = {}
print (my_dict)
{}

Enter fullscreen mode Exit fullscreen mode

You can change the values, for example:

my_dict["age"] = 31
Enter fullscreen mode Exit fullscreen mode

One use of dictionaries is counting how often we see something. You can use dictionaries to count the frequency of elements in a list or string. For example

counts = dict()
print (Enter a line of text: )
line = input ( )
words = line.split()
print (words:, words)
For word in words :
count [word] = counts.get(word, 0) + 1
print (count, counts)
Enter fullscreen mode Exit fullscreen mode

Also when working with API, dictionaries are often used to display JSON responses.
A list is a collection that is in an organized order while a dictionary is a lot of values each with its own label.

💖 💪 🙅 🚩
adewaleomosanya
adewaleomosanya

Posted on September 14, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

PYTHON DICTIONARIES
python PYTHON DICTIONARIES

September 14, 2023