6

So I have this list of dictionaries:

l = [{'COUNTRY': 'UK', 'STUDENT': 'JOHN'}, {'COUNTRY': 'PT', 'STUDENT':'PEDRO'}, {'COUNTRY': 'UK', 'STUDENT': 'KYLE'}, {'COUNTRY': 'IT', 'STUDENT':'PIRLO'}, {'COUNTRY': 'PT', 'STUDENT':'ANA'}, {'COUNTRY': 'FR', 'STUDENT':'VITO'}, {'COUNTRY': 'FR', 'STUDENT':'LOUIS'}]

I need to make a bar chart with the x axis being the countries that are on the list and and y axis being the number of times each country appears. My idea was to 'extract' those countries to a list, like this:

country_list = ['UK','PT','UK','IT','PT','FR','FR']

Then I would have to count how many times does each country appears and sort the list by frequency.

After all that is done I would have two lists:

country = ['UK','PT','FR','IT']

frequency = [2,2,2,1]

With these two lists I would be able to make the bar chart. How can I get those two lists from the original list of dictionaries?

Cardo20
  • 75
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [Group/Count list of dictionaries based on value](https://stackoverflow.com/questions/15815976/group-count-list-of-dictionaries-based-on-value) – Mel May 23 '17 at 10:01
  • 1
    Use `Counter` from `collections` module – kuro May 23 '17 at 10:02
  • I think I know how to use Counter to get the frequency of the items, but how would I remove the duplicates from the country_list without mixing up the order of the frequency list? – Cardo20 May 23 '17 at 10:09

3 Answers3

5

Having your list

l = [{'COUNTRY': 'UK', 'STUDENT': 'JOHN'}, {'COUNTRY': 'PT', 'STUDENT':'PEDRO'}, {'COUNTRY': 'UK', 'STUDENT': 'KYLE'}, {'COUNTRY': 'IT', 'STUDENT':'PIRLO'}, {'COUNTRY': 'PT', 'STUDENT':'ANA'}, {'COUNTRY': 'FR', 'STUDENT':'VITO'}, {'COUNTRY': 'FR', 'STUDENT':'LOUIS'}]

you could first extract all the values for the countries:

In [18]: k = [i['COUNTRY'] for i in l]

Then you could use the Counter from collections module:

In [19]: m = Counter(k)
Out[19]: Counter({'FR': 2, 'IT': 1, 'PT': 2, 'UK': 2})

To get the axes:

In [20]: countries = m.keys()

In [21]: frequency = m.values()

In [22]: countries
Out[22]: ['FR', 'PT', 'UK', 'IT']

In [23]: frequency
Out[23]: [2, 2, 2, 1]
elena
  • 3,740
  • 5
  • 27
  • 38
2
l = [{'COUNTRY': 'UK', 'STUDENT': 'JOHN'}, {'COUNTRY': 'PT', 'STUDENT':'PEDRO'}, {'COUNTRY': 'UK', 'STUDENT': 'KYLE'}, {'COUNTRY': 'IT', 'STUDENT':'PIRLO'}, {'COUNTRY': 'PT', 'STUDENT':'ANA'}, {'COUNTRY': 'FR', 'STUDENT':'VITO'}, {'COUNTRY': 'FR', 'STUDENT':'LOUIS'}]




myDict = {}

for d in l:
  c = d['COUNTRY']
  myDict[c] = myDict.get(c,0)+1
print(myDict)  

country = myDict.values()
frequency = myDict.keys()

print(country)
print(frequency)


=========

{'UK': 2, 'PT': 2, 'IT': 1, 'FR': 2}
dict_values([2, 2, 1, 2])
dict_keys(['UK', 'PT', 'IT', 'FR'])
Fuji Komalan
  • 1,979
  • 16
  • 25
2
#You can solve this problem easily by using lambda function:

country_list = list(map(lambda dict: dict['COUNTRY'], list))

# You can count the same data by using Counter function:

from collections import Counter 
counter = Counter(country_list)
counter = ({'UK': 2, 'PT': 2, 'FR': 2, 'IT': 1})
Krishna Kumar
  • 369
  • 5
  • 10