Dictionary in python

* What is a dictionary?

Keys vs. values:

Access a dictionary value by key or the get Method

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

Input:

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

Output:

True
False

Add or modify key value pair in a dictionary

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

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

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!

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