Dictionaries In Python

ProgrammingFreak
2 min readFeb 16, 2021
Dictionary in python

* What is a dictionary?

Also known as hash-map, map, associated array in other languages
Dictionaries are an unordered data structure for declaring relationships between objects. A mutable data structure consisting of pairs of keys and values. It is a value in any python object.

Keys vs. values:

keys are unique, values can be duplicates. keys must be immutable types(strings, integers, floats, tuples) values can be any data types

Dictionaries vs. Lists:
Both lists and dictionaries are mutable. A dictionary can have ley values pairs added, removed or modified. Unlike a list, a dictionary is not ordered. A dictionary is used for mappings while a list is used for order.

Access a dictionary value by key or the get Method

Input:

flight_prices = {
“Chicago”: 199,
“San Francisco”: 499,
“Denver”:295
}
print(flight_prices[“Chicago”])
print(flight_prices[“Denver”])

Output:

199
295

The In and NOt In operators in a dictionary

The in operator in Python checks whether a specified value is a constituent element of a sequence like string, array, list, or tuple etc. … When the specified value is found inside the sequence, the statement returns True . Whereas when it is not found, we get a False

Input:

print(“erm” in “watermelon”)
print(“z” in “watermelon”)

Output:

True
False

Add or modify key value pair in a dictionary

The dictionary type stores a key along with its value. The keys pair with values using a colon (:) while the commas work as a separator for the elements.

Input:

sports = {
“New England Patriots”: [“Tom”, “Rob”, “Julian”],
“Giants”:[“Eli”, “Odell”]
}
sports[“pittsburgh”]=[“Ben”, “Antonio”]
print(sports)

output:

{'New England Patriots': ['Tom', 'Rob', 'Julian'], 'Giants': ['Eli', 'Odell'], 'pittsburgh': ['Ben', 'Antonio']}

The set default Method

Python setdefault() method is used to set default value to the key. It returns value, if the key is present. Otherwise it insert key with the default value. Default value for the key is None.

Input:

film_directors = {
“The Godfather”: “Francis Ford Coppola”,
“The Rock”: “Michael Bay”,
“Goodfellas”: “Martin Scorsese”
}
print(film_directors.get(“Goodfellas”))
print(film_directors.get(“Bad Boys”, “Michael Bay”))
print(film_directors)

output:

Martin Scorsese
Michael Bay
{'The Godfather': 'Francis Ford Coppola', 'The Rock': 'Michael Bay', 'Goodfellas': 'Martin Scorsese'}

The Pop Method

Python List pop() The pop() method removes the item at the given index from the list and returns the removed item.

Input:

release_dates = {
“Python”: 1991,
“Ruby”: 1995,
“Java”: 1995,
“Go”: 2007
}
year=release_dates.pop(“Java”)
print(release_dates)
print(year)

output:

{'Python': 1991, 'Ruby': 1995, 'Go': 2007}
1995

Lets not make this article so big we will be continuing this topic in the next blog , hope you like this blog and put a like if you love it! Stay tuned! keep coding! Follow me on Twitter for more posts on python programming!

--

--

ProgrammingFreak

Arise, Awake and don’t stop until the goal is reached.