0

I have a dictionary in the format of male_name_counts = {male_name: male_name_counts}, where the KEY is male name and VALUE is the counts of appearance in my data.

I already know the most frequent name: 'highest_male_count' and I want to find all the corresponding keys with the value of the highest count.

I wrote a list comprehension:

top_male_names = [x for male_name_counts[x] == highest_male_count]

Then I got SyntaxError: invalid syntax. I am confused cuz I saw the official document for list comprehension says as long as there is a for with a iterable, it should be fine. I want to know what is wrong with this expression and is it possible to achieve my goal using just list comprehension?

Psyduck
  • 637
  • 3
  • 10
  • 22
  • 1
    Huh?!? There is only one key: the name with the highest count! You probably want the index location from your original data that contain the name with the highest count. Please read [MCVE] – Alexander Aug 23 '17 at 19:20
  • There is already implemented solution for this https://docs.python.org/2/library/collections.html#collections.Counter and the check the method most_common – Take_Care_ Aug 23 '17 at 19:21
  • Hey, I am sorry for the unclarity. There are lots of keys and values, what I wrote in the question is just a abbreviation of a dictionary. – Psyduck Aug 23 '17 at 19:21
  • @saintsfan342000 I found the answer in that post. Thanks – Psyduck Aug 23 '17 at 19:23
  • You could try: top_male_names = [x for x in male_name_counts if male_name_counts[x] == highest_male_count] – Dayana Aug 23 '17 at 19:25

0 Answers0