Home > Software engineering >  How to create a dictionary, where the key is the key of a dictionary of lists and the value is the l
How to create a dictionary, where the key is the key of a dictionary of lists and the value is the l

Time:05-07

I have this dictionary of lists of dictionaries:

dict_countries ={'uk': [{'datetime': '1955-10-10 17:00:00', 'city': 'chester'},{'datetime': '1956-09-10 13:00:00', 'city': 'london'}],'us': [{'datetime': '1974-10-10 23:00:00', 'city': 'hudson'}]}

So far I have wrote this function (I cannot use tuples). The funciton must return a dictionary where the Key is the name of the country with the most sights (dicctionaries inside its list) and the values is the number os sights :

def country_with_most_sights(dict_countries:dict)-> dict:

 record_dict = {}

for country in dict_countries 
 N_sights = len(each_country)
  if N_sights > record_dict[past_country]:
   past_country = ***country´s name*** 
      record_dict[past_country] = N_sights

return record_dict

The desired result will be record_dict to be:

record_dict = {uk:2}

CodePudding user response:

There are lots of ways you can do this. Here are a few:

  1. Find the lengths of each value, then find item that has the longest length using max and the key argument.

    def country_with_most_sights(dict_countries:dict)-> dict:
        return dict([max(((k, len(v)) for k, v in dict_countries.items()), key=lambda x: x[1])])
    

    Explanation:

    • Find the max() of the items() in your dictionary.
    • Each element of dict.items() is a tuple containing the key and value.
    • We convert this to a tuple containing the key and the length of the value using the generator expression ((k, len(v)) for k, v in dict_countries.items())
    • By specifying that lambda function, we use the the second element of this tuple (i.e. the length of the value) as the criterion to find the max.
  2. Use only the .keys() of the dict and index into the original dict using this key in your lambda.

    def country_with_most_sights(dict_countries:dict)-> dict:
        max_key = max(dict_countries.keys(), key=lambda k: len(dict_countries[k]))
        max_val = len(dict_countries[k])
        return {max_key: max_val}
    
  3. Iterate over the keys of your dict and keep track of the longest value. Then return that key and its length:

    def country_with_most_sights(dict_countries:dict)-> dict:
        longest_country = "none"
        num_sights = 0
        for k, v in dict_countries.items():
            if len(v) > num_sights:
                longest_country = k
                num_sights = len(v)
        return {longest_country: num_sights}
    

    Explanation:

    • Iterate over dict.items()
    • if the length of the value is more than anything you've seen before, save the key and value in the longest_country and num_sights variables.
    • After you're done with all items in the dict, create the return value from your variables and return it.

CodePudding user response:

dict_countries = {'uk': [{'datetime': '1955-10-10 17:00:00', 'city': 'chester'},
                         {'datetime': '1956-09-10 13:00:00', 'city': 'london'}],
                  'us': [{'datetime': '1974-10-10 23:00:00', 'city': 'hudson'}]}

val = 0
k = ''
for key in dict_countries.keys():
    qqq = len(dict_countries[key])
    if qqq > val:
        val = qqq
        k = key

ttt = {k: val}

Output

{'uk': 2}

Updating the maximum size of the lists, via the variable 'val'.

  • Related