Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding highest value in a dictionary

I'm new to programming and currently taking a CSC 110 class. Our assignment is to create a bunch functions that do all sorts of things with some data that is given. I have taken all that data and put it into a dictionary but I'm having some trouble getting the data I want out of it.

Here is my problem:

I have a dictionary that stores a bunch of countries followed by a list that includes their population and GDP. Formatted something like this

{'country': [population, GDP], ...}

My task is to loop through this and find the country with the highest population or GDP then print:

'The country with the highest population is ' + highCountry+\
    ' with a population of ' + format(highPop, ',.0f')+'.')

In order to do this I wrote this function (this one is specifically for highest population but they all look about the same).

def highestPop(worldInfo):
        highPop = worldInfo[next(iter(worldInfo))][0] #Grabs first countries Population
        highCountry = next(iter(worldInfo))#Grabs first country in worldInfo

        for k,v in worldInfo.items():
                if v[0] > highPop:
                    highPop = v[0]
                    highCountry = k

        return highPop,highCountry

While this is working for me I gotta think there is an easier way to do this. Also I'm not 100% sure how [next(iter(worldInfo))] works. Does this just grab the first value it sees?

Thanks for your help in advance!

Edit: Sorry I guess I wasn't clear. I need to pass the countries population but also the countries name. So I can print both of them in my main function.

like image 661
Luke Kelly Avatar asked Dec 04 '18 02:12

Luke Kelly


People also ask

How do you find the second highest value in a dictionary?

We can find the second largest value in a dictionary by sorting the values of the dictionaries and then retrieving the second last element from the sorted list.

How do you find the lowest value in a dictionary?

To find the minimum value in a Python dictionary you can use the min() built-in function applied to the result of the dictionary values() method.


2 Answers

I think you're looking for this:

max(worldInfo.items(), key=lambda x: x[1][0])

This will return both the country name and its info. For instance:

('france', [100, 22])

The max() function can work on python "iterables" which is a fancy word for anything that can be cycled or looped through. Thus it cycles or loops through the thing you put into it and spits out the item that's the highest.

But how does it judge which tuple is highest? Which is higher: France or Germany? You have to specify a key (some specification for how to judge each item). The key=lambda etc specifies a function that given an item (x), judge that item based on x[1][0]. In this instance if the item is ('france', [100, 22]) then x[1][0] is 100. So the x[1][0] of each item is compared and the item with the highest one is returned.

The next() and iter() functions are for python iterators. For example:

mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)

print(next(myit)) #=> apple
print(next(myit)) #=> banana
print(next(myit)) #=> cherry
like image 200
Conner Avatar answered Sep 25 '22 08:09

Conner


Use the max() function, like so:

max(item[0] for item in county_dict.values()) #use item[1] for GDP!

Also try storing the values not in a list ([a, b]) but in a tuple ((a, b)).

Edit: Like iamanigeeit said in the comments, this works to give you the country name as well:

max(data[0], country for country, data in country_dict.items())
like image 22
Eb946207 Avatar answered Sep 25 '22 08:09

Eb946207