Malcode
Malcode

Reputation: 301

Python : How to get the highest key from dictionary and once it found get the key/value?

hey guys I have a dictionary shows:

Shows = {177.17: ['SELECT * FROM `breaking.bad` '], 190.16: ['SELECT * FROM `breaking.bad`'], 81.72: ['SELECT * FROM `mad.men`'], 74.36: ['SELECT * FROM `game.of.thrones`'], 74.37: ['SELECT * FROM `game.of.thrones`']}

here I have a list I want to match if those keys values occur:

list = ['breaking.bad','mad.men','game.of.thrones','the.mandalorian']

Currently what i want to achieve is once the list value matches with the list in the dictionary I get the highest value of those keys along with its value .

What I have done so far is this :

for key ,value in Shows.items():
    for lst in list:
        if lst in value and 'breaking.bad' in value and max(key) :
            print(lst)

Any ideas that can help achieve my goals?

Upvotes: 0

Views: 53

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

Not the most efficient way, but loop over the "tables" first, then get the dictionary for each show and find the max

shows = {177.17: ['SELECT * FROM `breaking.bad` '], 190.16: ['SELECT * FROM `breaking.bad`'], 81.72: ['SELECT * FROM `mad.men`'], 74.36: ['SELECT * FROM `game.of.thrones`'], 74.37: ['SELECT * FROM `game.of.thrones`']}
tables = ['breaking.bad','mad.men','game.of.thrones','the.mandalorian']


for table in tables:  # don't name variables as list
    sub_shows = {key:value for key,value in shows.items() if any(table in v for v in value)}
    if sub_shows:  # max throws error if list is empty
        print(max(sub_shows), table)

Output

190.16 breaking.bad
81.72 mad.men
74.37 game.of.thrones

I question the use-case of having a list of only one item, or why you need SQL queries in your dictionary

Upvotes: 1

Related Questions