Python searching key returning values from list of dicts

I'm missing something and its driving me bananas,. For some reason, the lookup to the aliases list of dicts is not working.

import urllib.parse


def parse_source_from_url(url):
    # Clean up the url
    parse_url = urllib.parse.urlparse(url.strip().replace('\n', ''))
    
    # Parse the interesting parts
    url = parse_url.netloc.split('.')
    
    # Words to omit
    keywords = ['feeds', 'com']
    
    # Determine which url words are not keywords to omit
    feed_source = [u for u in url if u not in keywords][0]
    
    # Map out aliases as needed
    aliases = [
        {'feed_source': 'example', 'alias': 'good'},
        {'feed_source': 'feedburner', 'alias': 'notgood'}]
    
    # Select alias if feed_source is shown in list of dict
    # This is where the problem is
    feed_source = [
        a['alias']
        if a['feed_source'] == feed_source
        else feed_source
        for a in aliases]
    
    # Return only the aliased name
    return feed_source[0]

urls = ['https://feeds.example.com/', 'https://feeds.feedburner.com/']

for u in urls:
    test = parse_source_from_url(u)
    print(test)

Results:

  • The first result is correct ("example" aliased "good") but the second URL is incorrect.
  • "feedburner" should be aliased as "notgood"
good
feedburner

Tried:

  • The method here using next() and the get() methods.
  • Same issue

Solution 1:

You kinda miss the if placement in your loop. Now your feed_source list contains as much records as aliases list. And all of them are equal feed_source except eventually the one that has alias, but you takes first of it anyway.

Correct code should be:

    aliased = [a["alias"] for a in aliases if a["feed_source"] == feed_source]
    return aliased[0] if aliased else feed_source

So aliased has maximum one element now. If it, it's alias, if not (so aliased is empty) you just return feed_source

And one hint - if your aliases element always have two keys, maybe you could just use dict:

aliases = {'example': 'good', 'feedburner': 'notgood'}