A dictionary is a powerful data structure for storing data values in key value pairs.The commonly used method to access items in a dictionary is by referring the key name using the bracket notation; placing the key name inside square brackets. For example:
Car = {
'brand':'Nissan',
'model':'Versa',
'year':2012
}
'feed_title': d.get('feed', {}).get('title', 'No feed title available')
car['year']
will return 2012 as the year of manufacture.
The Get () Method
The .get() method is a powerful way of accessing the value for a given key if it exists in the dictionary.If the key does not exist, it returns a default value.
get() method syntax:
dictionary.get(key, default)
key is the key to search for and ‘default’ is the default value to return if the key is not found.
NOTE: If the ‘default’ is not specified, ‘None’ is returned. For example, Car.get(‘model’, ‘There is no model available’).
Therefore, the get() method is the most convenient way of retrieving a value from a dictionary without raising a ‘KeyError’ if the key is not found.
Real Application of get () Method
Problem Description
Recently, I was working on a RSS feed parser website.The problem emerged when I attempted to retrieve the feed title from the feed dictionary named d.
d = feedparser.parse(url)
entries = []
for item in d['entries']:
title = item.get('title', 'No title available')
description = item.get('description', 'No description available')
short_desc = short_description(description, 80)
link = item.get('link', 'No link available')
entries.append({
'title': title,
'description': short_desc,
'link': link
})
context = {
'entries':entries}
The above code block only retrieve a list of the title, summary and the link of the news article.The list is then converted to entries dictionary and stored in the context dictionary to be rendered to the template.
The main problem, was retrieving the name of the publishing newspaper. For example, CNN or the Standard, depending on the RSS feed link.
To solve this problem, it was imperative that I created a new dictionary with the key 'feed_title' .The value is the title of a feed, if it is available in the dictionary d.
context = {
'entries':entries,
'feed_title': d.get('feed',{}).get('title', 'No feed title available')
}
'feed_title': d.get('feed', {}).get('title', 'No feed title available')
This expression retrieves the value for the title key in the dictionary d nested under the key feed. If the key feed does not exist in the dictionary d, or if the key title does not exist under the key feed, the default value 'No feed title available' will be returned.
Explanation of how the code works:
• d.get ('feed', {}): This retrieves the value for the key feed in the dictionary d. It returns an empty dictionary {} if the key feed does not exist in the dictionary d.
• .get('title', 'No feed title available'): This retrieves the value for the key title in the dictionary returned by d.get('feed', {}). The default value 'No feed title available’ is returned if the key title does not exist in the dictionary.
Top comments (0)