Home > database >  how to create a function that conditionally queries a dictionary with list of values in Python?
how to create a function that conditionally queries a dictionary with list of values in Python?

Time:01-28

I have a dictionary that looks like this:

dict = {'Country': ['China','Diamond Princess','Singapore'],
 'Total Cases': [74187,621,81],
 'New Cases': [1751,79,0],
 'Total Deaths': [2006,0,0],
 'Recovered': [14796,17,29]}

I wonder how to create a function that returns a list of names of countries that have total cases above 100? Also, I wonder what if the filtering condition is based on other values? e.g., the proportion of New Cases over Total Cases greater than 10%? I can't seem to understand how to work with dictionaries in Python...any input is appreciated! Thanks!

CodePudding user response:

You can us list comprehension like so:

output = [dct['Country'][i] for i in range(len(dct['Country'])) if dct['Total Cases'][i] > 100]

Output:

['China', 'Diamond Princess'] #list of countries with more than 100 cases

Full code:

dct = {'Country': ['China','Diamond Princess','Singapore'],'Total Cases': [74187,621,81],'New Cases': [1751,79,0],'Total Deaths': [2006,0,0],'Recovered': [14796,17,29]}
output = [dct['Country'][i] for i in range(len(dct['Country'])) if dct['Total Cases'][i] > 100]
print(output)

CodePudding user response:

You have two lists, dict['Country'] and dict['Total Cases'], which store information about a single item in their corresponding elements.

If you zip them together, you can iterate over the corresponding pairs and select the country with a high case count.

def high_case_countries(d):
    return [country 
            for country, count in zip(d['Country'], d['Total Cases'])
            if count > 100]

x = high_case_countries(dict)

CodePudding user response:

You can iterate over the list of "Total Cases" and use its index to get the corresponding country name:

out = [dct['Country'][i] for i, num in enumerate(dct['Total Cases']) if num > 100]

Or use zip to traverse both lists together:

out = [country for num, country in zip(dct['Total Cases'], dct['Country']) if num > 100]

Output:

['China', 'Diamond Princess']

In general, you can use zip to traverse multiple lists together and use an if condition to filter the desired outcome.

So for example,

out2 = [] 
for country, new, total in zip(dct['Country'], dct['New Cases'], dct['Total Cases']):
    if new / total > 0.1:
        out2.append(country)

will fetch the countries where the new cases make up more than 10% of total cases.

Even though what you have is a dictionary, what matters is that you have a collection of lists where the indices correspond to each other. It's like a table with indices and columns. In table form, it looks like:

            Country  Total Cases  New Cases  Total Deaths  Recovered
0             China        74187       1751          2006      14796
1  Diamond Princess          621         79             0         17
2         Singapore           81          0             0         29
  •  Tags:  
  • Related