Safely accessing elements

Writing an if statement to check if a key exists in the dictionary and then access it might get repetitive and annoying. That repetitive checks can be removed when accessing the elements safely through a .get() method:
# Imagine we have a great team for a startup
team = {
	'HR': ['John Smith', 'Taylor Vu'],
	'Designers': ['Garry Tan'],
	'Developers': ['Linus Torvalds', 'Yegor Bugayenko'],
if 'HR' in team:

if 'QA' in team:
print(team.get('HR', []))
print(team.get('QA', []))
Here we access the team dictionary with a .get() method, where we provide the key as a first parameter (HR or QA), and a default value as a second parameter (an empty list in this example).
Another example could be accessing a dictionary with some default value like a number or a string: population.get('China', '~1.5 Billion'). If key China exists in the dictionary population, the program will return its corresponding value. But if China is not in the population dictionary, the program will return '~1.5 Billion'.


Antonym dictionary contains words that have opposite meanings. Given n antonym word pairs, you are asked to print q antonyms for the q words that follow, and Not found if those words are not found in the initial list. It’s guaranteed that the words in antonym pairs are unique.
The first line of the input contains a single integer n which is the number of antonym pairs. The next n lines contain two words separated by a space that represents the antonym pairs. The next line contains a single integer q - the number of query words. The next q lines contain a single word.
The program should print q lines. Each line should contain the antonym or Not found if the antonym is not present in the initial list.
5 warm cold sunny cloudy fast slow tired energetic love hate 3 warm hate potato
cold love Not found


Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue