freddyvh
freddyvh

Reputation: 11

Use a dictionary-comprehension to return country as key and a total number of cities for country as value

I'm using a dict-comprehension to return country names as unique key and want value to be number of cities in that country. How do I count the number of cities from list of tuples?

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

country_names = { 

}

Expect result to be: {'Italy': 2 , 'Netherlands': 4, 'Spain': 4}

Upvotes: 0

Views: 227

Answers (5)

N Chauhan
N Chauhan

Reputation: 3515

Use a sum with a generator that returns 1 if the country name matches the country being checked, otherwise 0:

{name: sum(1 if c[0] == name else 0
           for c in country_city_tuples)
 for name in set(c[0] for c in country_city_tuples)}

You can also use dict.get:

r = {}
for name, city in country_city_tuples:
    r.get(name, 0) += 1

Upvotes: 1

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

You can extract out the names of the countries from the list of tuples using zip and then use collections.Counter to counter the frequency of country names

from collections import Counter

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

#Extract out country names using zip and list unpacking
country_names, _ = zip(*country_city_tuples)

#Count the number of countries using Counter
print(dict(Counter(country_names)))

To do it without using collections, we can use a dictionary to collect the frequencies

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

#Extract out country names using zip and list unpacking
country_names, _ = zip(*country_city_tuples)

result = {}

#Count each country
for name in country_names:
    result.setdefault(name,0)
    result[name] += 1

print(result)

The output will be same in both cases

{'Netherlands': 4, 'Spain': 4, 'Italy': 2}

Upvotes: 3

sahasrara62
sahasrara62

Reputation: 11238

without using defaultdict and other modules

country_city_tuples= [('Netherlands', 'Alkmaar'),
                      ('Netherlands', 'Tilburg'),
                      ('Netherlands', 'Den Bosch'),
                      ('Netherlands', 'Eindhoven'),
                      ('Spain', 'Madrid'),
                      ('Spain', 'Barcelona'),
                      ('Spain', 'Cordoba'),
                      ('Spain', 'Toledo'),
                      ('Italy', 'Milano'),
                      ('Italy', 'Roma')]

country_names ={}
for i in country_city_tuples:
    try:
        if country_names[i[0]]:
            country_names[i[0]]+=1
    except:
        country_names[i[0]]=1
print(country_names)

#output {'Netherlands': 4, 'Spain': 4, 'Italy': 2}

Upvotes: 0

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Try this :

l = set(i[0] for i in country_city_tuples)
d = {}
for i in l:
   d[i] = sum([1 for j in country_city_tuples if j[0]==i])

Output :

{'Italy': 2, 'Netherlands': 4, 'Spain': 4}

Upvotes: 1

Space Impact
Space Impact

Reputation: 13255

Using defaultdict:

from collections import defaultdict

country_names  = defaultdict(int)
for i in country_city_tuples:
    country_names[i[0]]+=1

country_names
defaultdict(int, {'Netherlands': 4, 'Spain': 4, 'Italy': 2})

Upvotes: 2

Related Questions