0

I have created a dictionary in python. I have sorted the dictionary with the following instruction.

dict = {}

dict[identifier] = dst
sorted_dict = sorted(dict.items(), key=operator.itemgetter(1))
print sorted_dict

Here identifier is key and dst is a value

I want to retrieve first N elements from the dictionary. How I can do that?

cswah
  • 401
  • 1
  • 5
  • 22
  • 3
    `print(sorted_dict[:N])`? – Aran-Fey May 25 '18 at 08:03
  • 1
    wouldnt it be easier to use `Counter`? – Netwave May 25 '18 at 08:04
  • 1
    @Netwave it would, which is why I'm disappointed the answer below parroting Aran-Fey's comment has 2 upvotes and no one's offered a Counter-based solution yet. – cs95 May 25 '18 at 08:06
  • slicing works. thanks @Aran-Fey – cswah May 25 '18 at 08:06
  • that shows brassiness. If an answer can get 2 upvote, why not the question – cswah May 25 '18 at 08:07
  • duplicate questions can be deleted? @Aran-Fey – cswah May 25 '18 at 08:14
  • 1
    @cswah we optimise for pearls, not sand. – cs95 May 25 '18 at 08:14
  • @cswah You're asking if you should delete your question? You don't have to; it can be a useful signpost. – Aran-Fey May 25 '18 at 08:16
  • @cswah No, duplicates are good to broaden the target area for the general problem with this solution. Broaden for people googling, I mean. – Arne May 25 '18 at 08:17
  • thanks!!!. Can't understand why it is voted down. – cswah May 25 '18 at 08:17
  • 1
    @cswah downvoting is used to filter questions from the frontpage. The people who did so probably meant to convey 'this question is a duplicate, don't bother looking at it.' It doesn't feel good, but it's not meant to judge you, just a tool to manage this site. – Arne May 25 '18 at 08:20
  • 1
    @cswah don't worry about the downvotes, here on SO people with high rep thinks they own this site and do whatever they want to do without even reading q, because of these kind of people So is becoming hard for new users – akash karothiya May 25 '18 at 08:24
  • 2
    Some people (not me) downvote common duplicate questions that are easy to find with a simple search, because it shows that you didn't do much research before posting your question. – PM 2Ring May 25 '18 at 08:26
  • 1
    @akashkarothiya As Arne said, duplicate _questions_ aren't so bad, because once they're closed they can help people to find the linked "target" page. But _answers_ on duplicate questions are not so good because it scatters the answers over many pages, making them harder to find and harder to compare. And it means that the answers aren't fairly competing with each other for votes. – PM 2Ring May 25 '18 at 08:29
  • I totally understand, however my question more generic in nature than already answered question. – cswah May 25 '18 at 08:30
  • Agreed @PM2Ring thanks for clarifying :) – akash karothiya May 25 '18 at 08:31
  • @akashkarothiya You may enjoy reading [my Meta SO post](https://meta.stackoverflow.com/a/322110/4014959) on this topic. ;) – PM 2Ring May 25 '18 at 08:36
  • 1
    @PM2Ring much helpful, you are such a gem (I admire you alot) :) – akash karothiya May 25 '18 at 08:41

2 Answers2

2

Use slicing to extract n elements of the list

>>> print(sorted_dict[:n])
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
2

collectons.Counter It's the way to go:

from collections import Counter
count_dict = Counter(the_dict)
print(count_dict.most_common(n))

Here you have a live example

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • 2
    This should be the accepted answer. Also note that the Counter works best with numeric values, and you only need to create once instead of sorting repeatedly. – cs95 May 25 '18 at 08:10
  • @coldspeed, yes, actually, does it works on O(n) right? – Netwave May 25 '18 at 08:11
  • 1
    Yes, for any k < n, it should return elements from a heap in O(k) time. – cs95 May 25 '18 at 08:12