Conditionally replacing items in nested list based on values in dictionary
Question: Right now I have this nested list and dictionary:

    
    
    phrases = [['Lionhearted', 'Baby Preach'], ['Lionhearted Baby', 'Preach'], ['Lionhearted', 'Baby', 'Preach']]
    artist_info = {u'Baby': u'Baby by Justin Bieber', u'Lionhearted': u'Lionhearted by Porter Robinson', u'Preach': u'Preach by Drake', u'Baby Preach': u:'Baby Preach by some pop singer'}
    

Basically, what I would like to do is try to replace the values in phrases
with the values from artist_info if there is a match, other wise I would like
to omit that particular nested list. So since there is a dictionary entry for
`'Lionhearted'`, `'Baby'`, 'baby preach' and `'Preach'`, the output would read
something like this:

    
    
    phrases2 = [['Lionhearted by Porter Robinson','Baby Preach by some pop singer'] ['Lionhearted by Porter Robinson', 'Baby by Justin Bieber', 'Preach by Drake']]
    

Is there a way to do this? I've been at this for hours. It's important that
the three phrases result in the order of "Lionhearted Baby Preach". I'm a bit
of a Python noob, and I haven't been able to find a comparable example.


Answer: Is this what you are looking for? From your example it looks like you want to
omit the sublist within `phrases` if any item of that sublist is not present
in the dictionary `artist_info`. The code below does that before doing the
replacements you mentioned.

    
    
    phrases = [['Lionhearted', 'Baby Preach'], ['Lionhearted Baby', 'Preach'], ['Lionhearted', 'Baby', 'Preach']]
    artist_info = {u'Baby': u'Baby by Justin Bieber', u'Lionhearted': u'Lionhearted by Porter Robinson', u'Preach': u'Preach by Drake', u'Baby Preach': u'Baby Preach by some pop singer'}
    
    lst = filter(lambda x: all(y in artist_info for y in x), phrases)
    phrases2 = map(lambda x: [artist_info[y] for y in x], lst)
    print phrases2
    

