I have a large list of rss feed items that I'm attempting to sort by key 'published_parsed' like so:
allheadlines.sort(key=lambda hl:hl[0]['published_parsed'], reverse=True)
The problem is this is returning a key error as some feed items do not have the key published_parsed. I tried changing to updated_parsed but again, some feed items do not have the key updated_parsed. After interchanging the two in the solution given below:
allheadlines.sort(key=lambda hl:hl[0].get('published_parsed', hl[0].get('updated_parsed')), reverse=True)
I have another problem. There is an rss feed that's not returning one or both of these keys. How would I sort everything that do have at least 1 of these keys and do nothing for the items that do not have either of these keys.
Try using dict.get
Ex:
allheadlines.sort(key=lambda hl:hl[0].get('published_parsed', hl[0].get('updated_parsed')), reverse=True)